__del__ not working with cyclic reference? (and memory-leaked?)

Erik Max Francis max at alcyone.com
Wed Jul 2 02:31:27 EDT 2003


Jane Austine wrote:

> For example, say the singleton has a database connection, in a cgi
> program.
> I want the singleton clear itself(and close the connection) before
> the whole process exits. It seems to be more convenient and
> object-oriented.

Typically singleton patterns need an out-of-band signal to tell them to
destroy and reclaim the (protected) singleton instance.  Usually it's
done with some kind of 'close' or 'finish' method.

Note that there are other patterns that are useful for singleton-like
behaviors in Python; say, for instance, the Borg pattern (where each
"instance" really just has a reference to the original instance's
__dict__ contents).

> Then "destructor" has less use than in other environments.
> Maybe I should avoid destructors in python as well as I can.

Not really.  In C++, destructors are only called when an object is
properly deleted.  In Java, finalize methods have the same guarantees as
Python's __del__ method; they're not guaranteed to be called in a timely
fashion or at all.

> It is neat and reliable. Thanks.

Sure thing.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ When the solution is simple, God is answering.
\__/  Albert Einstein




More information about the Python-list mailing list