[Python-Dev] chained assignment weirdity
Guido van Rossum
guido at python.org
Tue Nov 6 16:37:45 CET 2012
+1 to what Nick said. And I thought about this carefully when
designing the language. It's not a bug. The note about assignment RHS
being evaluated before LHS is normative -- you just have to interpret
RHS as "after the *last* '=' symbol". Assignment itself is *not* an
On Tue, Nov 6, 2012 at 4:01 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> As noted, it's really only counterintuitive if your intuition is primed to
> expect C style right to left chained assignments.
> Python, on the other hand, is able to preserve primarily left to right
> evaluation in this case with only the far right hand expression needing to
> be evaluated out of order.
> One example that can really make the intended behaviour clear:
> *a = *b = iter(range(3))
> a ends up as (0,1,2), b ends up as (), because the first assignment consumes
> the entire iterable.
> My actual advice, though? If the order of assignment really matters, use
> multiple assignment statements rather than relying on readers knowing the
> assignment order.
> Sent from my phone, thus the relative brevity :)
> Python-Dev mailing list
> Python-Dev at python.org
--Guido van Rossum (python.org/~guido)
More information about the Python-Dev