Unbound names in __del__

Peter Hansen peter at engcorp.com
Sat Jun 18 12:13:08 EDT 2005


Torsten Bronger wrote:
> Peter Hansen <peter at engcorp.com> writes:
>>What's your use case for del?
> 
> Every instance represents a "session" to a measurement instrument.
> After the instance is deleted, the session should be closed to free
> resources.

You mean like GPIB devices?  We've written a lot of software that talks 
to instruments, as well as pumps, motors, and sensors of all kinds.  I 
haven't even needed to "free resources", other than by closing a serial 
port, for example.  Such simple operations don't require the use of 
__del__ and can easily be done with simple .close() type calls as the 
application shuts itself down or finishes a test sequence or other 
operation.

Use of __del__ is, in my opinion, a bit of a crutch (meaning it seems to 
make life easier, but then you start relying on it and find it hard to 
do without).  Given that it isn't really reliable in non-trivial 
situations, I'd recommend pretending __del__ does not exist and 
restructuring your system to close these sessions explicitly, under your 
direct control, at the appropriate point.  This has worked very well for 
us so far.

-Peter



More information about the Python-list mailing list