Notification when reference count goes to 0?

Daniel Dittmar daniel at dittmar.net
Mon Dec 23 04:05:46 EST 2002


Robert Oschler wrote:
> Is there a way to get notification when the reference count goes to 0 on an
> object? The only thing I miss from C++ is destructors.  If there was a way
> to get notified when an object's reference count went to 0, the automatic
> release of associated resources would be possible, yes?  Just to be
> explicit, I'm not interested in garbage collection based finalization, as
> everyone knows the timing or even the occurrence of such an event comes
> under the heading of 'unspecified behavior', at least with Java it is.

In adition to the __del__method mentioned in other posts, you'll 
probably want to know about the 'try ... finally' statement. The finally 
block is guaranteed to execute , regardless of there being an exception 
thrown or not. If you want to mimic the destruction of stack based 
objects when they get out of scope, this would be the way according to 
the Python language specification.

Daniel





More information about the Python-list mailing list