问题描述
解包/ splat运算符 *
和 **
在python版本中的适用性差异很大(2.7,3 .x< 3.5和3.x> = 3.5)。
例如:
| 2.7 | 3.1-3.4 | 3.5
--------------------------------------------- -------------------------
函数(*参数)✓✓✓
x,* y,z = [1,2,3,4,5] x✓✓
{** x,** y} xx✓
我错过的各种版本之间是否还有差异?我正在浏览PEP和Readmes,但文档没有详细说明。
1992年左右(不确定Python版本)。这来自的语法文件。
#06-Apr-92:
#只用'*'表示可变长度列表
#31-Mar -92:
#收紧try语句的语法
#27-Feb-92:
#在eval输入后允许NEWLINE *
#16- 1月-92:
#在可变参数语法中添加了'*'作为'+'的替代
#(不确定哪种方法更好。)
#11-Jan- 92:
#添加变量长度参数列表语法:def f(a,b,+ rest):...
:
:
在Python 3.0中删除了元组参数解包。
据我所知,没有列出所有语法变化的单个页面。每个版本的语法更改都列在部分,或者您可以检查每个版本的语法规范以查看差异。
The unpacking/splat operators *
and **
differ widely in their applicability across python versions (2.7, 3.x < 3.5 and 3.x >= 3.5).
For example:
| 2.7 | 3.1-3.4 | 3.5
----------------------------------------------------------------------
function(*args) ✓ ✓ ✓
x, *y, z = [1, 2, 3, 4, 5] x ✓ ✓
{**x, **y} x x ✓
Are there any more discrepancies between the various versions that I've missed? I'm looking through PEP and Readmes but the docs aren't detailed with this.
Around 1992 (not sure about Python version). This is from the Grammar file of Python 1.0.1.
# 06-Apr-92:
# Use only '*' for varargs list
# 31-Mar-92:
# Tighten syntax for try statements
# 27-Feb-92:
# Allow NEWLINE* after eval input
# 16-Jan-92:
# Added '*' as alternative for '+' in varargs syntax
# (Not sure which alternative is better yet.)
# 11-Jan-92:
# Variable length argument list syntax added: def f(a, b, +rest): ...
Tuple parameter unpacking was removed in Python 3.0.
As far as I know there's no single page that lists all the syntax changes. Per version syntax changes are listed in the What's new in Python section or you could check the Grammar specification of each release to see the differences.
这篇关于python的splat运算符*和**在哪里有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!