#` setattr ` is a fairly advanced magic
1 messages ยท Page 1 of 1 (latest)
Thx for pointing me to FancyLed and the Animations!
I found a great example of what I wanted to do earlier:
# How to use properties and a setter
class Led():
def __init__(self, brightness):
self._brightness = brightness
@property
def brightness(self):
return self._brightness
@brightness.setter
def brightness(self, brightness):
self._brightness = brightness
print("Something else can happen here...")
if __name__ == '__main__':
led = Led(100)
led.brightness = 200
print(led.brightness)
ah. that's probably more appropriate than __setattr__(). i guess __setattr__() is more for when you have a possibly large set of attributes that you want to customize the behavior for, and you don't want to (or can't) create a property for each one
note that the NeoPixel class already does some similar stuff. it's effectively using __setitem__() in a way that indexing into a NeoPixel instance to update LED colors can automatically update the physical LED states (if auto_write is on)
ahh, I'm definitely getting closer nodding all the loose ends together. ๐
Thx for the hint with auto write. I'm actually using circuitpython atm and I thinks it's not available there.
Could you probably give me an example how I could have used __setitem__ to achieve the same as I've done with the properties?
i'm pretty sure that the CircuitPython NeoPixel class has an auto_write boolean property
oh wait, where are you importing neopixel from? you didn't show that in your earlier code
are you using CircuitPython or MicroPython?
Im using an esp32 wemos d1 mini with micropython.