'How can one instance of an object to effect al other instances of that object
I help maintain a python robot interface library. The library interfaces with some embedded processors containing a bug which we have no ability to patch and so have to come up with a work around in our python API.
The bug is if you do:
R.gpio[1].mode = OUTPUT
R.gpio[2].mode = OUTPUT
R.gpio[3].mode = OUTPUT
R.gpio[4].mode = OUTPUT
and then later
R.gpio[2].mode = INPUT
GPIO's 2 get set as INPUT but also 3 and 4. The embedded processor will be okay if all of the GPIO's are set in order every time.
My current thoughts are to cache the current state of the GPIO's in python then every time each gpio's mode is updated to send the signals to update them all in order so even if you change 2, 1 gets updated then 2 then 3 etc.
I can either:
Have a method on R.gpio's (which inherits from
list) which somehow gets called when this occurs, but how do I know whenmodeis set on the contained objects? I only know__getattr__has been called.Pass a reference to the
GPIOListclass to each of theGPIOobjects in the list, then call a method on theGPIOListclass to update each of the methods. BUT then I've coupled the interfaces of the encapsulating object and the encapsulee.
Is there a pythonic way (maybe classmethods) to avoid either of these catches?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
