[Python-ideas] clear() method for lists

Gerald Britton gerald.britton at gmail.com
Thu Feb 11 18:35:08 CET 2010


On Thu, Feb 11, 2010 at 10:38 AM, Tim Lesher <tlesher at gmail.com> wrote:
>
>
> 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>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>
>

Thanks all for helping me understand this better.  The subtly above is
something I missed.  I searched the doc for a description of it but
couldn't readily find it.  Tim's simple one-line statement and the
example above does it very nicely.

Switching gears for a moment, what is the feeling regarding the copy()
methods for dictionaries and sets?  Are they truly redundant?  Should
they be deprecated?  Should users be encouraged to use the copy module
or just  use "newdict = dict(olddict)" and "newset = set(oldset)" to
build a new dictionary or set from an existing one?

-- 
Gerald Britton



More information about the Python-ideas mailing list