pyvisa / pyvisa-py

A pure python PyVISA backend
https://pyvisa-py.readthedocs.io
MIT License
282 stars 120 forks source link

Question: Instrument-specific class method? #341

Closed Moooongo closed 1 year ago

Moooongo commented 1 year ago

Suppose I have a USB instrument connected, I can use inst = rm.open_resources("some_resource"), which returns a USBInstrument instance. If I would like an instrument-specific method, my first idea was to derive a sub-class or child-class from the class pyvisa.resources.usb.USBInstrument and implement my new method there. However, since the initialization is handled by the resource manager, I cannot specify that I want a custom subclass to be returned. Because I have only a generic USBInstrument class, any subsequent function calls (that are not defined in the class) need to include the instrument instance.

It is not the end of the world, but it is not elegant. Is there a better way of doing this?

MatthieuDartiailh commented 1 year ago

Typically when using VISA you are better off using composition over inheritance. Most instruments support multiple interface typeS (GPIB, USB and TCPIP) for example. If you were to inherit from the USB class you would be unable to use your driver with the other supported interface types. So I recommend you store the resource on your object and forward communications through it.