Python equivalent of C++'s virtual destructors?

Erik Max Francis max at alcyone.com
Mon Jul 31 14:51:37 EDT 2000


I have a class which has to do cleanup when it goes out of scope (and so
has a __del__ method), but which is also going to be derived and its
subclasses may also need to do class-specific things on destruction as
well.  What is the accepted way of handling this situation?  The
solution which presents itself most easily is simply to explicitly call
the destructor of the base class in the derived class:

class Base:
    def __del__(self):
        print "in base destructor"


class Derived(Base):
    def __del__(self):
        print "in derived destructor"
        Base.__del__(self)


d = Derived()
d = None

However, I distinctly recall reading somewhere that explicitly calling a
__del__ method for a class is a big no-no.

What's the generally accepted way of accomplishing this in Python?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Love is the most subtle form of self-interest.
\__/ Holbrook Jackson
    Fat Boy and Little Man / http://www.fatboyandlittleman.com/
 Watch Fat Boy and Little Man go about their antics.



More information about the Python-list mailing list