简体   繁体   English

为什么 Python 海象运算符不是分隔符?

[英]Why is the Python walrus operator not a delimiter?

The Python walrus operator ( := ) is listed in the documentation as an operator , but not as a delimiter like the rest of the assignment operators (eg += ). Python walrus 运算符 ( := ) 在文档中列为operator ,但不像赋值运算符的 rest 那样作为分隔符 (例如+= )。 Why then is the walrus operation not also a delimiter?那么为什么海象操作不是分隔符呢?

You write “the rest of the assignment operators”, but there is only one assignment operator in Python, and that's the walrus operator.你写了“赋值运算符的rest”,但是Python中只有一个赋值运算符,那就是海象运算符。 The other assignment... thingies are delimiters in an assignment statement .另一个赋值...东西是赋值语句中的分隔符。

The main reasoning for why its not allowed is to avoid ambiguity in code.为什么不允许它的主要原因是避免代码中的歧义。 More exceptional cases from PEP 572 – Assignment Expressions PEP 572 中的更多例外情况——赋值表达式

Unparenthesized assignment expressions are prohibited at the top level of an expression statement.在表达式语句的顶层禁止使用无括号的赋值表达式。 Example:例子:

 y:= f(x) # INVALID (y:= f(x)) # Valid, though not recommended

This rule is included to simplify the choice for the user between an assignment statement and an assignment expression – there is no syntactic position where both are valid.包含此规则是为了简化用户在赋值语句和赋值表达式之间的选择——没有语法 position 两者都有效。

Unparenthesized assignment expressions are prohibited at the top level of the right hand side of an assignment statement.在赋值语句右侧的顶层禁止使用无括号的赋值表达式。 Example:例子:

 y0 = y1:= f(x) # INVALID y0 = (y1:= f(x)) # Valid, though discouraged

Again, this rule is included to avoid two visually similar ways of saying the same thing.同样,包含此规则是为了避免用两种视觉上相似的方式说同一件事。

Unparenthesized assignment expressions are prohibited for the value of a keyword argument in a call.对于调用中的关键字参数的值,禁止使用无括号赋值表达式。 Example:例子:

 foo(x = y:= f(x)) # INVALID foo(x=(y:= f(x))) # Valid, though probably confusing

This rule is included to disallow excessively confusing code, and because parsing keyword arguments is complex enough already.包含此规则是为了禁止过度混淆代码,并且因为解析关键字 arguments 已经足够复杂。

Unparenthesized assignment expressions are prohibited at the top level of a function default value. function 默认值的顶层禁止使用无括号赋值表达式。 Example:例子:

 def foo(answer = p:= 42): # INVALID... def foo(answer=(p:= 42)): # Valid, though not great style...

This rule is included to discourage side effects in a position whose exact semantics are already confusing to many users (cf. the common style recommendation against mutable default values), and also to echo the similar prohibition in calls (the previous bullet).包含此规则是为了防止 position 中的副作用,其确切语义已经让许多用户感到困惑(参见反对可变默认值的通用样式建议),并且还呼应了调用中的类似禁令(上一个项目符号)。

Unparenthesized assignment expressions are prohibited as annotations for arguments, return values and assignments.禁止带括号的赋值表达式作为 arguments、返回值和赋值的注解。 Example:例子:

 def foo(answer: p:= 42 = 5): # INVALID... def foo(answer: (p:= 42) = 5): # Valid, but probably never useful...

The reasoning here is similar to the two previous cases;这里的推理与前两种情况类似; this ungrouped assortment of symbols and operators composed of: and = is hard to read correctly.这种由: 和 = 组成的未分组的各种符号和运算符很难正确阅读。

Unparenthesized assignment expressions are prohibited in lambda functions. lambda 函数中禁止使用无括号赋值表达式。 Example:例子:

 (lambda: x:= 1) # INVALID lambda: (x:= 1) # Valid, but unlikely to be useful (x:= lambda: 1) # Valid lambda line: (m:= re.match(pattern, line)) and m.group(1) # Valid

This allows lambda to always bind less tightly than:=;这允许 lambda 始终绑定不如:=; having a name binding at the top level inside a lambda function is unlikely to be of value, as there is no way to make use of it.在 lambda function 内部的顶层具有名称绑定不太可能有价值,因为没有办法使用它。 In cases where the name will be used more than once, the expression is likely to need parenthesizing anyway, so this prohibition will rarely affect code.在名称将被多次使用的情况下,表达式可能无论如何都需要括号,所以这个禁令很少会影响代码。

Assignment expressions inside of f-strings require parentheses. f 字符串内的赋值表达式需要括号。 Example:例子:

 >>> f'{(x:=10)}' # Valid, uses assignment expression '10' >>> x = 10 >>> f'{x:=10}' # Valid, passes '=10' to formatter ' 10'

This shows that what looks like an assignment operator in an f-string is not always an assignment operator.这表明在 f 字符串中看起来像赋值运算符的东西并不总是赋值运算符。 The f-string parser uses: to indicate formatting options. f-string 解析器使用: 来表示格式化选项。 To preserve backwards compatibility, assignment operator usage inside of f-strings must be parenthesized.为了保持向后兼容性,f-strings 内的赋值运算符用法必须用括号括起来。 As noted above, this usage of the assignment operator is not recommended.如上所述,不建议使用赋值运算符。

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

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