python assignment

Tim Peters tim.one at comcast.net
Wed Jul 23 16:24:21 EDT 2003


[Tim]
>> It would be very unusual (because bad design) for the __iadd__
>> method of a mutable type to return a pre-existing object, though.

[Juha Autero]
> I'm confused. I thought that the idea of __iadd__ is that for
> *mutable* types it modifies existing object instead of returning new
> one.

Bjorn added more appropriate words -- it would be bad design for the
__iadd__ method of a mutable type to return a pre-existing object other than
self.

>>> a = [1, 2]
>>> b = [1]
>>> b += [2]

While "a == b" must be true at this point, it would be a nightmare if "a is
b" could be true at this point.  For immutable types it doesn't matter:

>>> a = (1, 2)
>>> b = (1,)
>>> b += (2,)

It so happens that "a is b" is not true at this point under any Python
released so far, but it could be true someday without causing harm.

Sorry for the confusion!






More information about the Python-list mailing list