list.clear() missing?!?
Alan Morgan
amorgan at xenon.Stanford.EDU
Wed Apr 12 19:43:52 EDT 2006
In article <pan.2006.04.12.23.23.07.830018 at REMOVETHIScyber.com.au>,
Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:
>On Wed, 12 Apr 2006 15:36:47 -0700, Alan Morgan wrote:
>
>> Serious question: Should it work more like "s=[]" or more like
>> "s[:]=[]". I'm assuming the latter, but the fact that there is
>> a difference is an argument for not hiding this operation behind
>> some syntactic sugar.
>
>Er, I don't see how it can possibly work like s = []. That merely
>reassigns a new empty list to the name s, it doesn't touch the existing
>list (which may or may not be garbage collected soon/immediately
>afterwards).
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.
Alan
--
Defendit numerus
More information about the Python-list
mailing list