Calling A Deconstructor

sismex01 at hebmex.com sismex01 at hebmex.com
Fri Nov 15 09:27:21 EST 2002


> From: John Abel [mailto:john.abel at pa.press.net]
> Sent: Friday, November 15, 2002 7:47 AM
> 
> Hi,
> 
> I have a class, with an __init__, and a main function, basically a 
> wrapper around smtplib.  I wish to add a __del__ to the class.  Two 
> questions:  Do I need to do it this way?  If so, how do I can 
> close the instance, so the __del__ is called?  The script will be
> permanently resident, so I wanted to make sure that I didn't have
> various connections open to the smtp server.
> 
> Thanks
> 
> John
>

Hmm... interesting conundrum.

An object is "deleted" automatically when the last reference
to it disappears (goes out of scope, is deleted explícitly,
etc).  BUT, you can't force the __del__ method to be called,
it's an implementation detail that depends on the memory
management scaffolding in place.

For example.  In CPython, memory management is done via
reference counting; when the refcount of an object drops
to zero, it's immediately destructed and deallocated; BUT,
in Jython, memory management is done via Java's machinery,
and there is no assurement of *when*, or *if*, the
destructor is going to be called on the object.

So, for sanity's sake, use a .close() method, or a .done(),
or something like that, to explicitly close your connection
to your SMTP server, so you won't have to depend on __del__
doing it for you.

Python doesn't have the fixed semantics for destructor 
invocation that C++ has, so you have to take that into account
when designing your software.

HTH

-gustavo

pd: TGIF!




More information about the Python-list mailing list