[Tutor] detecting data member changes

Luke Paireepinart rabidpoobear at gmail.com
Tue Apr 24 08:42:14 CEST 2007


Supposing I have a class like this:

class wiimote(object):
    def __init__(self):
        self.leds = [0,0,0,0]
    def updateLEDs(self):
        do_stuff()#write correct output report to wiimote to toggle LEDs.

So basically what I want to do is that whenever the self.leds variable 
is changed, the updateLEDs method is called.
( I can't call it constantly in a loop because 1) the wii remote freaks 
out, and 2) that seems pretty ineffiicent.)
I know I could use a __setattr__ but then I'd have to access the leds 
using a dictionary with an 'LED' key mapped to the status array.
It's not necessarily a bad thing for my own use, but it seems like it'd 
be off-putting to other people using my library.
So is there a way to detect such changes that's Pythonic?
Thanks,
-Luke


More information about the Tutor mailing list