list.clear() missing?!?

Fredrik Lundh fredrik at pythonware.com
Tue Apr 11 14:25:25 EDT 2006


John Salerno wrote:

> Steven Bethard wrote:
>
>
> >     lst[:] = []
> >     lst = []
>
> What's the difference here?

L[:]= modifies the object in place, L=[] binds the variable to a
new object.  compare and contrast:

>>> L = ["a", "b", "c"]
>>> M = L
>>> L
['a', 'b', 'c']
>>> M
['a', 'b', 'c']
>>> L is M
True
>>> L[:] = []
>>> L
[]
>>> M
[]
>>> L is M
True

>>> L = ["a", "b", "c"]
>>> M = L
>>> L
['a', 'b', 'c']
>>> M
['a', 'b', 'c']
>>> L = []
>>> L
[]
>>> M
['a', 'b', 'c']
>>> L is M
False

</F>






More information about the Python-list mailing list