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

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Jun 3 13:08:30 EDT 2006


Piet van Oostrum a écrit :
>>>>>>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

Not necessarily:

 >>> a = range(10)
 >>> b = a
 >>> id(a)
1078034316
 >>> id(b)
1078034316
 >>> a is b
True
 >>> a = []
 >>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> id(a)
1078043724
 >>> id(b)
1078034316
 >>>

What as been deleted is the association ('binding') between the name 'a' 
and the list object at 1078034316. This object won't be suppressed until 
there are no more names bound to it.

> and created a new one, which is different
> from keeping the list and deleting the elements:

indeed !-)




More information about the Python-list mailing list