pitfall for your amusement
Bryan Richard
bryan at pyzine.com
Tue Nov 12 15:21:51 EST 2002
Andrew Koenig wrote:
>>> >>> x = (1, 2, 3)
>
>>> >>> x += (4, 5, 6)
>
> Gustavo> Hey, shouldn't this raise an exception?
>
> Perhaps it should, but it doesn't: If x is a tuple,
>
> x += y
>
> is equivalent to
>
> x = x + y
>
> which creates a brand-new object and (re)binds x to it.
>
I had never heard of this pitfall, so I had to run it.
>>> id(x)
135374140
>>> id(y)
135321812
>>> id (x1)
135373676
>>> id(y1)
135373676
Stupid question: Is object 135321812 destroyed if I now run
y += (4, 5, 6)
- Bryan
More information about the Python-list
mailing list