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 02:26:03 CEST 2006


python a écrit :
> after del list , when I use it again, prompt 'not defined'.how could i
> delete its element,but not itself?

FWIW, del don't delete an object (not directly at least), it just delete 
the name<->object association. If (and only if) it was the only name 
referencing that object, then the object will be disposed too.

 >>> list1 = [1, 2]
 >>> list2 = list1
 >>> id(list1)
1078043852
 >>> id(list2)
1078043852
 >>> del list2
 >>> list1
[1, 2]
 >>> list2
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
NameError: name 'list2' is not defined
 >>>



More information about the Python-list mailing list