Unbound names in __del__

Peter Hansen peter at engcorp.com
Sat Jun 18 12:59:33 EDT 2005


Torsten Bronger wrote:
>     keithley = GpibInstrument(14)
>     keithley.write("*IDN?")
>     print keithley.read()
> 
> A keithley.close() would be a wart in my opinion; instead I want to
> hide the whole session thing from the programmer.  Besides, I
> haven't yet given up the hope that the issues with __del__ can be
> tackled.

At least one alternative comes to mind.  Have the GpibInstrument class 
(or its module) register an atexit() method, and have the constructor 
for that class track all instances.  On shutdown, the atexit method goes 
through all instruments that are still open and issues the .close() 
requests, or whatever you do in the __del__ now.

In other words, it would be indistinguishable from __del__ from the 
users' point of view, at the cost of a little extra code to make things 
explicit, instead of relying on the implicit and, unfortunately, 
unreliable nature of __del__.  (Which is probably the real wart in 
Python, unfortunately.)

-Peter



More information about the Python-list mailing list