the deceptive continuous assignments

Yingjie Lan lanyjie at yahoo.com
Tue Dec 6 06:06:04 EST 2011


Hi, I just figured out this with Python3.2 IDLE:

>>> class k: pass
>>> x=k()
>>> x.thing = 1
>>> x.thing
1
>>> x = x.thing = 1
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    x = x.thing = 1
AttributeError: 'int' object has no attribute 'thing'
>>> x
1
>>>

================
when I do x=x.thing=1, I thought it would
be like in C, 1 is first assigned to x.thing,
then it is further assigned to x.

But what seems to be going on here is
that 1 is first assigned to x, then
to x.thing (which causes an error).

Any reason why would Python deviate
from C in this regard?

Thanks!

Yingjie



More information about the Python-list mailing list