Python equivalent of C++'s virtual destructors?

david_ullrich at my-deja.com david_ullrich at my-deja.com
Tue Aug 1 12:48:05 EDT 2000


In article <3985CAB9.4928BDDE at alcyone.com>,
  Erik Max Francis <max at alcyone.com> wrote:
> 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:
[...]
>
> However, I distinctly recall reading somewhere that explicitly calling
a
> __del__ method for a class is a big no-no.

   Can you recall where you read this and exactly what it said?
In the docs (under "Basic Customization") it says

"__del__(self) Called when the instance is about to be destroyed.
If a base class has a __del__ method the derived
class's __del__ method must explicitly call it to ensure
proper deletion of the base class part of the instance.
e.g. ''BaseClass.__del__(self)''."

which would seem to indicate it's not a no-no at all.

    You might want to note what else it says:

 "Note that it is possible (though not recommended!) for the
__del__ method to postpone destruction of the instance by
creating a new reference to it. It may then be called at a
later time when this new reference is deleted. It is not
guaranteed that __del__ methods are called for objects
that still exist when the interpreter exits."

The last sentence is news to me - it's not clear to me
what a person _is_ supposed to do to guarantee things
get cleaned up when the interpreter exits.

DU


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list