Problem with __del__

Aahz Maruch aahz at panix.com
Tue Jan 9 19:59:13 EST 2001


In article <r3N66.1718$_e7.114959 at news2.atl>,
Joel Ricker <joejava at dragonat.net> wrote:
>
>I guess I could chime in with my question.  What is the proper
>implementation of deleting objects once you are done with them.  Thats
>probably the one part of the objects section of the tutorial  that I wasn't
>sure about. Wouldn't you want a destructor to do things for cleanup or would
>just implement it differently like this:
>
>def deleteme(self)
>    dosomething();
>    del self

Depends what you mean by "cleanup".  One of Python's axioms is "explicit
is better than implicit", so if by "cleanup" you for example mean that a
special series of three bytes should be written to a file at the end of
the operation, you're responsible for writing your program logic in a way
that writes the bytes at the appropriate time.

OTOH, if by "cleanup", you simply mean reclaiming the used memory, you
mostly don't need to worry about it in Python.  When objects go out of
scope, they're automatically cleaned up, more or less.  The only time
they don't get automatically cleaned up is when you've got a cyclical
reference (two objects that mutually point at each other).  Even then,
Python 2.0 now comes with garbage collection enabled by default, so it's
not too likely you'll have problems.
-- 
                      --- Aahz (Copyright 2001 by aahz at pobox.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

Sometimes, you're not just out of left field, you're coming in all the
way from outer space.  --Aahz



More information about the Python-list mailing list