after del list , when I use it again, prompt 'not defined'.how could i delete its element, but not itself?

Piet van Oostrum piet at cs.uu.nl
Fri Jun 2 18:53:14 EDT 2006


>>>>> SuperHik <junkytownMAKNI at gmail.com> (S) escribió:

>S> bearophileHUGS at lycos.com wrote:
>>> python wrote:
>>>> after del list , when I use it again, prompt 'not defined'.how could i
>>>> delete its element,but not itself?
>>> 
>>> This is a way:
>>>>>> a = range(10)
>>>>>> del a[:]
>S> or simply
>S> a = []
>>>>>> a
>>> []

Then you *have* deleted the list and created a new one, which is different
from keeping the list and deleting the elements:

>>> a = range(10)
>>> b = a
>>> del a[:]
>>> a
[]
>>> b
[]
>>> a.append(100)
>>> a
[100]
>>> b
[100]
>>> a=[]
>>> b
[100]
>>> 
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list