On Sunday, November 20, 2016 at 6:47:35 AM UTC-5, Bev in TX wrote: > From the Python 3.5.2 docs: > > 6.15. Evaluation order > Python evaluates expressions from left to right. Notice that while > evaluating an assignment, the right-hand side is evaluated before the > left-hand side. > > Thus, spam = eggs = cheese = obj is equivalent to: > > spam = (eggs = (cheese = obj)) > > which is also equivalent to: > > cheese = obj > eggs = cheese > spam = eggs
This is not right. Python will evaluate the right-hand side, and then assign that value to all of the names, from left to right. So the end result is equivalent to: spam = obj eggs = obj cheese = obj The reason the doc quote above doesn't apply here is that assignment is not an expression, but a statement of its own, described in 6.2: "An assignment statement evaluates the expression list (remember that this can be a single expression or a comma-separated list, the latter yielding a tuple) and assigns the single resulting object to each of the target lists, from left to right." You can see this by disassembling some examples: >>> import dis >>> def f(): ... a = b = c = expr() ... >>> dis.dis(f) 2 0 LOAD_GLOBAL 0 (expr) 2 CALL_FUNCTION 0 4 DUP_TOP 6 STORE_FAST 0 (a) 8 DUP_TOP 10 STORE_FAST 1 (b) 12 STORE_FAST 2 (c) 14 LOAD_CONST 0 (None) 16 RETURN_VALUE >>> def g(): ... d[a()] = d[b()] = d[c()] = expr() ... >>> dis.dis(g) 2 0 LOAD_GLOBAL 0 (expr) 2 CALL_FUNCTION 0 4 DUP_TOP 6 LOAD_GLOBAL 1 (d) 8 LOAD_GLOBAL 2 (a) 10 CALL_FUNCTION 0 12 STORE_SUBSCR 14 DUP_TOP 16 LOAD_GLOBAL 1 (d) 18 LOAD_GLOBAL 3 (b) 20 CALL_FUNCTION 0 22 STORE_SUBSCR 24 LOAD_GLOBAL 1 (d) 26 LOAD_GLOBAL 4 (c) 28 CALL_FUNCTION 0 30 STORE_SUBSCR 32 LOAD_CONST 0 (None) 34 RETURN_VALUE Of course, it hardly ever matters, because how often does anyone use multiple assignment, and even rarer, where the targets are computed, and even rarer, where the order of their assignment matters? --Ned. -- https://mail.python.org/mailman/listinfo/python-list