[Python-ideas] clear() method for lists

Tim Lesher tlesher at gmail.com
Thu Feb 11 16:38:22 CET 2010


On Thu, Feb 11, 2010 at 10:21, Matthew Russell <matt.horizon5 at gmail.com>wrote:

> this seems to work in python 2.x and python3.1, although I suspect it's a
> bug.
>
> >>> t = (1, 2)
> >>> t += (3,)
> >>> t
> (1, 2, 3)
>
> The object "t" references at the end isn't the same one that it references
at the beginning.  Note the difference between lists and tuples here:

>>> a = [1,2]
>>> id(a)
11274840
>>> a += [3,]
>>> id(a)
11274840

a is a list; augmented assignment mutates it, but it's still the same
object.

>>> b = (1,2)
>>> id(b)
13902872
>>> b += (3,)
>>> id(b)
13915800
>>>

b is a tuple; augmented assignment creates a new object and re-binds "b" to
it.

-- 
Tim Lesher <tlesher at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20100211/19d165da/attachment.html>


More information about the Python-ideas mailing list