What does "del" actually do?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Feb 13 18:46:04 EST 2007


John Nagle a écrit :
>    The Python "reference manual" says, for "del", "Rather that spelling 
> it out in full details, here are some hints."  That's not too helpful.
> 
>    In particular, when "del" is applied to a class object, what happens?
> Are all the instance attributes deleted from the object?  

It would have been simpler to just test:
 >>> class Ghost(object):
...     def __init__(self, name):
...             self.name = name
...     say = "whoo"
...     def greetings(self):
...             print "%s from %s %s" \
...             %(self.say, self.__class__.__name__, self.name)
...
 >>> g1 = Ghost("Albert")
 >>> g2 = Ghost("Ivan")
 >>> g1.greetings()
whoo from Ghost Albert
 >>> del Ghost
 >>> g1.greetings()
whoo from Ghost Albert
 >>> g1.__class__
<class '__main__.Ghost'>
 >>>

the del statement remove a name from the current namespace. period. It's 
also used for removing keys from dicts.

> Is behavior
> the same for both old and new classes?

Should it be different ?

>    I'm trying to break cycles to fix some memory usage problems.

GC and/or Weakrefs should do.



More information about the Python-list mailing list