[Python-ideas] clear() method for lists
gerald.britton at gmail.com
Thu Feb 11 15:51:42 CET 2010
On Thu, Feb 11, 2010 at 3:08 AM, Simon Brunning
<simon at brunningonline.net> wrote:
> 2010/2/10 Gerald Britton <gerald.britton at gmail.com>:
>> Lastly, for completeness, I suppose copy() might be appropriate for
>> both tuple and deque as well.
> Why would you want to copy a tuple?
Say you had a problem where you started with a basic tuple, then
needed to add items to it to produce some result. Now suppose you
want to do that repeatedly. You don't want to disturb the basic
tuple, so you make a copy of it before extending it.
>>> country = ("US",)
>>> country_state = tuple(country)+("NY",)
>>> country_state_city = tuple(country_state) + ("NY",)
('US', 'NY', 'NY')
if tuple() had a copy() method, I could write:
country_state = country.copy() + ("NY",)
Not that this is necessarily "better" in some way. I'm just thinking
about consistency across the built-in types. If dict() and set() have
copy(), why not list() and tuple()?
On the other hand, if the consensus is _not_ to add the copy() method
to lists and tuples, why not deprecate the method in sets and dicts
and encourage folks 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?
> Simon B.
> Python-ideas mailing list
> Python-ideas at python.org
More information about the Python-ideas