Best practice: Sharing object between different objects

Paul Rubin no.email at nospam.invalid
Sat Feb 21 12:18:57 EST 2015


pfranken85 at gmail.com writes:
> ADC, DAC components. As I said, I would like to derive the
> corresponding classes from one common class, let's say I2CDevice, so
> that they can share the same bus connection (I don't want to do a
> import smbus, ..., self.bus = smbus.SMBus(1) all the time). 

I don't really understand the question but it sounds like you might
want a class variable:

    import smbus

    class I2CDevice
       bus = smbus.SMBus(1)
       def __init__ (self): ...

This puts the "bus" object into the I2CDevice class itself.  It is
created when the I2CDevice class is defined.  Methods wanting to access
it can then say

   do_something_with(I2CDevice.bus)

is that what you wanted?



More information about the Python-list mailing list