Unbound names in __del__

Torsten Bronger bronger at physik.rwth-aachen.de
Sat Jun 18 12:55:07 EDT 2005


Hallöchen!

Peter Hansen <peter at engcorp.com> writes:

> 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?

Yes.

> 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.  [...]

I've just finished a thin-wrappers implementation of VISA in Python,
see <http://pyvisa.sf.net/>.  It calls functions in a proprietary
VISA DLL/SO.  The next step is to build a simple-to-use OO layer on
top of it.  Therefore, we don't communicate with the device
directly, but via sessions (=handles) within the DLL.

These sessions should be freed when the object instance representing
the device is destroyed by Python.  Using the __del__ method is the
natural choice for this in my opinion.

> [...] 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.

I'd find this quite sad because so far it's drop-dead simple to use
the OO layer, and I may even convince our in-house HT Basic fans of
Python:

    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.

Tschö,
Torsten.

-- 
Torsten Bronger, aquisgrana, europa vetus



More information about the Python-list mailing list