Dumb question on assignment chaining

Tim Peters tim.one at comcast.net
Mon Mar 3 20:24:14 EST 2003


[Manus Hand]
> ...
> So it seems that in the construction x = y = z, the assignments take
> place in left-to-right order (i.e., x = z and then y = z;

Yes.

> the opposite of how the C language implements its assignment operation).
> I checked the Python documentation to see if this is etched in stone and
> can be depended upon,

Yes.

> but I saw nothing (am I just missing it?)

Yes.

> in section 6.3.

Try reading the second sentence again <wink>:

    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.

In your

    x = y = z

example, z is "the expression list", and "x" and "y" are two "target lists",
which are assigned to (as it says) left to right.  This follows from the
grammar rule given there:

    assignment_stmt ::= (target_list "=")+ expression_list

In your example, the

    target_list "="

part is expanded twice.






More information about the Python-list mailing list