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

Jane Austine janeaustine50 at hotmail.com
Wed Jul 2 08:11:23 CEST 2003

> Jane Austine wrote:
> > I read the gc module reference thoroughly and
> > thanks to your short comment, I could understand it.
> > (I should not use __del__ for singleton patterns, but
> > any other alternatives?)
> I'm not sure why you'd want to use __del__ for a singleton pattern in
> the first place, since singleton patterns are supposed to cushion the
> creation and destruction of the singleton object away from the interface
> user.  What are you trying to do with singleton patterns as it relates
> to __del__?

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

> However, Python doesn't make any guarantees about how timely __del__
> will be called, or indeed even if it will be (in the case of circular
> references).  If you're finding yourself needing to rely on using

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

> __del__ to get rid of important objects (such as those that are attached
> to significant resources), a far better solution would be to use try:
> ... finally: clauses surrounding the usage of such objects:
> resource = Resource()
> try:
>     ... use resource ...
> finally:
>     resource.close()

It is neat and reliable. Thanks.

> -- 
>    Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
>  __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
> /  \ Men and women, women and men.  It will never work.
> \__/  Erica Jong

More information about the Python-list mailing list