list.clear() missing?!?
Alan Morgan
amorgan at xenon.Stanford.EDU
Wed Apr 12 21:06:01 EDT 2006
In article <mailman.4481.1144888259.27775.python-list at python.org>,
Peter Hansen <peter at engcorp.com> wrote:
>Alan Morgan wrote:
>> Right. I was wondering what would happen in this case:
>>
>> s=[1,2,3]
>> t=s
>> s.clear()
>> t # [] or [1,2,3]??
>>
>> If you know your basic python it is "obvious" what would happen
>> if you do s=[] or s[:]=[] instead of s.clear() and I guess it is
>> equally "obvious" which one s.clear() must mimic. I'm still not
>> used to dealing with mutable lists.
>
>If you know your basic python :-), you know that s[:] = [] is doing the
>only thing that s.clear() could possibly do,
Ah, but if you know your basic python then you wouldn't be looking for
s.clear() in the first place; you'd just use s[:]=[] (or s=[], whichever
is appropriate). IOW, the people most in need of s.clear() are those
least likely to be able to work out what it is actually doing.
Personally, it seems more *reasonable* to me, a novice python user,
for s.clear() to behave like s=[] (or, perhaps, for s=[] and s[:]=[] to
mean the same thing). The fact that it can't might be an argument for
not having it in the first place.
Alan
--
Defendit numerus
More information about the Python-list
mailing list