#` setattr ` is a fairly advanced magic

1 messages ยท Page 1 of 1 (latest)

wise gust
#

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)
spiral gyro
#

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)

wise gust
#

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?

spiral gyro
#

oh wait, where are you importing neopixel from? you didn't show that in your earlier code

#

are you using CircuitPython or MicroPython?

wise gust
#

Im using an esp32 wemos d1 mini with micropython.