python的splat运算符*和**在哪里有效? [英] Where are python's splat operators * and ** valid?

查看:321
本文介绍了python的splat运算符*和**在哪里有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解包/ 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版本)。这来自 Python 1.0.1 的语法文件。

 #06-Apr-92:
#只用'*'表示可变长度列表

#31-Mar -92:
#收紧try语句的语法

#27-Feb-92:
#在eval输入后允许NEWLINE *

#16- 1月-92:
#在可变参数语法中添加了'*'作为'+'的替代
#(不确定哪种方法更好。)

#11-Jan- 92:
#添加变量长度参数列表语法:def f(a,b,+ rest):...

Python 1.4+


关键字参数:用Python编写的函数和方法现在可以是使用表单的关键字参数调用的
keyword = value


Python 1.6+


<现在可以使用特殊的语法来代替 apply()
函数。 f(* args,** kwds)相当于 apply(f,args,kwds)。您
也可以使用变化 f(a1,a2,* args,** kwds),您可以保留一个
或另一个: f(* args) f(** kwds)


Python< = 2.7



在Python 3.0中删除了元组参数解包。


PEP 3113 删除元组参数
解包。
您可以不再写 def foo(a,(b,c)):...
使用 def foo(a,b_c):b,c = b_c 代替。

Python 3.0+


PEP 3132 扩展的Iterable
解包
。您现在可以编写诸如 a,b,* rest =
some_sequence
之类的东西。甚至 * rest,a = stuff 。其余的对象总是
a(可能是空的)列表;右边可以是任何可迭代的。


PEP 3102 关键字参数在参数列表中出现在
* args 之后的命名参数必须在调用中使用关键字语法指定。您也可以在参数列表中使用裸露的 * 来指示
,您不接受可变长度参数列表,但是您有
关键字参数


Python 3.5+


PEP 448 ,额外的
解包概括。







据我所知,没有列出所有语法变化的单个页面。每个版本的语法更改都列在 Python中的新增内容部分,或者您可以检查每个版本的语法规范以查看差异。


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): ...

Python 1.4+:

Keyword Arguments: Functions and methods written in Python can now be called using keyword arguments of the form keyword = value.

Python 1.6+

There's now special syntax that you can use instead of the apply() function. f(*args, **kwds) is equivalent to apply(f, args, kwds). You can also use variations f(a1, a2, *args, **kwds) and you can leave one or the other out: f(*args), f(**kwds).

Python <= 2.7:

Tuple parameter unpacking was removed in Python 3.0.

PEP 3113: Tuple parameter unpacking removed. You can no longer write def foo(a, (b, c)): .... Use def foo(a, b_c): b, c = b_c instead.

Python 3.0+

PEP 3132: Extended Iterable Unpacking. You can now write things like a, b, *rest = some_sequence. And even *rest, a = stuff. The rest object is always a (possibly empty) list; the right-hand side may be any iterable.

PEP 3102: Keyword-only arguments. Named parameters occurring after *args in the parameter list must be specified using keyword syntax in the call. You can also use a bare * in the parameter list to indicate that you don’t accept a variable-length argument list, but you do have keyword-only arguments

Python 3.5+

PEP 448, additional unpacking generalizations.


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运算符*和**在哪里有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆