Unexpected result for list operator "+="

Fredrik Lundh fredrik at effbot.org
Fri Jan 5 03:14:15 EST 2001


Greg Jorgensen wrote:
> The real distinction--that assignment operations on immutable objects
> create new objects (or bind a reference to a different existing object)

Careful.

Ordinary assignment operations (a = b) bind names in a name-
space, they never create new objects.

Things that may look as assignment operations, but are really
syntactic sugar for method calls, may do whatever they want:

    a[x] = b # short for __setitem__
    a.x = b # short for __setattr__
    a += b # short for __iadd__
    a.append(b)

There's no distinction between mutable and immutable objects
in Python itself.  An object is immutable only if it doesn't have
any methods that allow you to change its content.  Neither you
nor Python can do an "ismutable(x)" test.

Cheers /F





More information about the Python-list mailing list