Dumb question on assignment chaining

Manus Hand manus at diplom.org
Mon Mar 3 20:13:46 EST 2003


Okay, first of all, I know that for clarity's sake I should write
statements like a = a.b = c in two statements, but in playing
around....  :-)

class Node:
    def __init__(self, val): self.val, self.next = val, None
    def __repr__(self): return `self.val`

walker = zero = Node(0)
walker = walker.next = Node(1)
print zero.next     # this prints None
#  ...hmm. Okay, start over...
walker = zero = Node(0)
walker.next = walker = Node(1)
print zero.next     # this prints 1

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; 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, but I saw nothing (am I just missing it?) in section 6.3.
...In fact, I saw nothing about chained assignments in that section
(where I would have thought it would be) at all.

So to my "dumb question" -- is this behavior indeed etched in stone?
And if so, where can I find the stone tablet? (Not in section 6.3 of
the language reference, unless I am mistaken.)

Just curious,
Manus




More information about the Python-list mailing list