Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: __del__ not working with cyclic reference? (and memory-leaked?)

Reply
Thread Tools

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

 
 
Jane Austine
Guest
Posts: n/a
 
      07-02-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
object-oriented.

[snip]
>
> 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 && http://www.velocityreviews.com/forums/(E-Mail Removed) && 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

 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      07-02-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 && (E-Mail Removed) && 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: super not working in __del__ ? Christopher J. Bottaro Python 5 02-18-2005 12:58 AM
Re: super not working in __del__ ? Fredrik Lundh Python 5 02-16-2005 01:28 PM
super not working in __del__ ? Christopher J. Bottaro Python 0 02-15-2005 11:34 PM
File Unlocking in __del__ does not work schwerdy Python 1 11-10-2004 03:54 PM
Re: __del__ not working with cyclic reference? (and memory-leaked?) Jane Austine Python 2 07-02-2003 02:36 PM



Advertisments