pitfall for your amusement
Bernhard Herzog
bh at intevation.de
Wed Nov 13 06:01:08 EST 2002
Erik Max Francis <max at alcyone.com> writes:
>
> L = [1, 2, 3]
> L += [4, 5, 6]
>
> is the equivalent of
>
> L = [1, 2, 3]
> L.extend([4, 5, 6])
Not quite. It's more like
L = [1, 2, 3]
L = L.__iadd__([4, 5, 6])
The rebinding as always done. So += can sometimes fail for a list:
>>> t = ([1,2,3],)
>>> t[0] += [4, 5, 6]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
Of course, the list already has been modified in place:
>>> t
([1, 2, 3, 4, 5, 6],)
Bernhard
--
Intevation GmbH http://intevation.de/
Sketch http://sketch.sourceforge.net/
MapIt! http://www.mapit.de/
More information about the Python-list
mailing list