简体   繁体   English

python参数解包在哪里属于操作顺序?

[英]Where does python argument unpacking fall into the order of operations?

http://docs.python.org/2/reference/expressions.html#operator-precedence http://docs.python.org/2/reference/expressions.html#operator-precedence

My guess is that it falls into one of the buckets above dict lookups since我的猜测是它属于 dict 查找之上的桶之一,因为

func(*mydict[mykey])

does the dictionary lookup first.首先进行字典查找。 Is there a better chart than my initial link that goes into more detail regarding order of operations in python?有没有比我的初始链接更好的图表,它更详细地介绍了 Python 中的操作顺序?

The unpacking * is not an operator;解包*不是运算符; it's part of the call syntax.它是调用语法的一部分。 It's defined under Calls , where you can see that:它在Calls下定义,您可以在其中看到:

["," "*" expression]

… can be part of an argument_list in two different places. ... 可以在两个不同的地方成为argument_list一部分。 (The semantics are described in the paragraphs starting "If there are more positional…" and "If the syntax…".) (语义在“如果有更多位置……”和“如果语法……”的段落中进行了描述。)

So it takes any expression .所以它需要任何expression You can see that no operator takes a full expression as its direct argument.您可以看到没有运算符将完整expression作为其直接参数。 So, if you want to loosely consider * an operator, it binds more loosely than any operator.所以,如果你想松散地考虑*一个运算符,它比任何运算符都更松散地绑定。 But just remember that it isn't actually an operator.但请记住,它实际上不是运算符。

Also keep in mind that this was all changed in Python 3.x.还要记住,这一切都在 Python 3.x 中改变了。 But the basic idea is the same—both argument unpacking and assignment unpacking take an expression , not just a primary , and therefore loosely-speaking bind more loosely than any operator, which all take a primary or something more specific.但基本思想是相同的——参数解包和赋值解包都采用expression ,而不仅仅是primary ,因此松散地说绑定比任何运算符都更松散,它们都采用primary或更具体的东西。


Meanwhile, you might want to try running the parser on your code to see what it does:同时,您可能想尝试在您的代码上运行解析器以查看它的作用:

>>> import ast
>>> tree = ast.parse('func(*mydict[mykey])')
>>> ast.dump(tree)
"Module(body=[Expr(value=Call(func=Name(id='func', ctx=Load()), args=[], keywords=[],
starargs=Subscript(value=Name(id='mydict', ctx=Load()),
slice=Index(value=Name(id='mykey', ctx=Load())), ctx=Load()), kwargs=None))])"

You can see that the entire Subscript expression ends up as the starargs to the Call .您可以看到整个Subscript表达式最终作为Callstarargs

The ast module uses the Abstract Grammar rather than the one described in the reference manual. ast模块使用抽象语法而不是参考手册中描述的语法 It has different names for things, and doesn't handle some things that are considered part of the grammar but actually done at a higher level than the parser, and so on—but, on the other hand, it's a lot easier to take in all at once.它对事物有不同的名称,并且不处理一些被认为是语法的一部分但实际上在比解析器更高的级别上完成的事情,等等——但是,另一方面,它更容易接受一次全部。 You can see that an expr used for starargs can be a Subscript .您可以看到用于starargsexpr可以是Subscript

As BrenBarn mentioned in the comments, the unpacking is defined as part of function calls (Python 2 and 3) and assignment statements (Python 3).正如 BrenBarn 在评论中提到的,解包被定义为函数调用(Python 2 和 3)和赋值语句(Python 3)的一部分。

So no, it will never take part of the operator precedence because it isn't an operator.所以不,它永远不会成为运算符优先级的一部分,因为它不是运算符。

How do I know that this won't try to unpack "mydict" in this example?我怎么知道在这个例子中这不会尝试解压“mydict”? Or is this something that the language parser handles.或者这是语言解析器处理的东西。

In your example, func(*mydict[mykey]) , the specification for the function call applies.在您的示例func(*mydict[mykey]) ,函数调用的规范适用。 So let's try to parse that manually.所以让我们尝试手动解析它。

The base part is matched by the definition of call , so *mydict[mykey] is the argument_list .基本部分与call的定义匹配,因此*mydict[mykey]argument_list And in the argument list, it will be parsed as "*" expression , with mydict[mykey] being the expression.在参数列表中,它将被解析为"*" expression ,其中mydict[mykey]是表达式。 As such, the parser will never apply the unpacking first, because the grammar simply doesn't specify a case where after "*" expression another part in brackets follows.因此,解析器永远不会首先应用解包,因为语法根本没有指定在"*" expression之后括号中的另一个部分的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM