Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这是我现在写的代码:
a = 1 if (a := a + 1) == 2: print(a)
我想知道是否存在这样的事情:
a = 1 if (a +:= 1) == 2: print(a)
PEP-527定义了新的海象运算符。讨论赋值语句和表达式之间差异的部分明确指出:
不支持增强分配: total += tax # Equivalent: (total := total + tax)
不支持增强分配:
total += tax # Equivalent: (total := total + tax)
在解释为什么仍然需要的部分中,我们发现:=:=
=
:=
这两种形式具有不同的灵活性。:= 运算符可以在更大的表达式中使用;= 语句可以扩展为 += 及其朋友,可以链接,并且可以分配给属性和下标。
这强烈暗示不打算支持海象和任何类型的就地运营商的合并。