5 ms·
The PEP is here: https://www.python.org/dev/peps/pep-0572/ https://www.python.org/dev/peps/pep-0572/ "This is a proposal for creating a way to assign to variab
by _Codemonkeyism 8y ago
The PEP is here: https://www.python.org/dev/peps/pep-0572/ https://www.python.org/dev/peps/pep-0572/
"This is a proposal for creating a way to assign to variables within an expression using the notation NAME := expr."
- Flimm 8y agoPrevious discussion on Hacker News: https://news.ycombinator.com/item?id=16949248 https://news.ycombinator.com/item?id=16949248
- hiisukun 8y agoAn in case it sees more recent updates, the github copy (where Guido proposes people can submit changes) is here: https://github.com/python/peps/blob/master/pep-0572.rst https://github.com/python/peps/blob/master/pep-0572.rst
- mci 8y ago> x = y = z = 0 # Equivalent: (x := (y := (z := 0))) This comment is false. It should say x = y = z = 0 # Equivalent: (z := (y := (x := 0))) "...assigns the single resulting object to each of the target lists, from left to right." https://docs.python.org/3/reference/simple_stmts.html#assignment-statements https://docs.python.org/3/reference/simple_stmts.html#assign... Here is a demonstration of the difference: >>> class Node: pass ... >>> node = blue_node = Node() >>> red_node = Node() >>> node = node.next = red_node >>> blue_node.next is red_node Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Node instance has no attribute 'next'
- jessaustin 8y agoI don't see how that demonstrates anything about the order of the assignment. It seems to show that node and blue_node are no longer the same object? (I'm not sure I understand how that came to be either, but perhaps it's due to the PEP...) I'm not saying your general point is wrong, just that the REPL listing doesn't demonstrate your general point.
- jwilk 8y ago"node" is initially blue, and then you do: node = node.next = red_node If assignments were right-to-left, the .next attribute would be set on the blue node. If they were left-to-right, it would be added to the red node. The AttributeError exception shows it is the latter order.
- fractalb 8y agoIt doesn't say anything about the order of assignment. It just shows that `node.next` wasn't evaluated to anything. `node.next` needs to be evaluated before doing any assignments
- jjnoakes 8y agoSure it does. Write out the example with both orders of assignments (convert it to single assignments only) and run it both ways.
- jwilk 8y agoYou are technicaly correct, but... If x, y, z are just variables, the order of assignments doesn't make any practical difference. OTOH, complex assignment targets (such as "a[i]" or "a.b") are not supported for := assignments.