list.clear() missing?!?
Peter Hansen
peter at engcorp.com
Wed Apr 12 20:30:30 EDT 2006
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, which is changing the
contents of the list which has the name "s" bound to it (and which might
have other names bound to it, just like any object in Python). It
*cannot* be doing the same as "s=[]" which does not operate on the list
but creates an entirely new one and rebinds the name "s" to it.
The only possible answer for your question above is "t is s" and "t ==
[]" because you haven't rebound the names.
-Peter
More information about the Python-list
mailing list