Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to adress a attribute created by setattr()?

I want to create a Micropython-Class for an LED-Strip. When initializing, the class can be called with any number of color=Pin pairs.
So my idea was to call def __init__(self, **kwargs) and then use:

for color, pin in kwargs.items():
    setattr(self, color, PWM(Pin(pin)))

I want to refer later (e.g. to change the PWM-value with self.color.duty(). My problem is, I don’t know, how to create any kind of list of all created attributes, so I can iterate through them later on.
Thinking about something like:

for color in color_list:
    color.duty(number_gained_via_mqtt)

But how can I put the created attributes into this "color_list"?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Update

Thank you for the first Answers! The Problem is, a simple list of the keys does not solve my problem. I can’t call self.key_from_list (as far as I know).

E.g. if I create a liste from keys color_list=["r","g","b","w"]I can’t (or don’t know how to) iterate through them.

for color in color_list:
    self.color.duty(0)

will not work, because the list only contains strings and I don’t know, how to call attributes of my class with that name (if that makes any sense)

The full code looks like this:

class Stripe():
    def __init__(self, **kwargs):
        self.on = 1
        self.leds = dict()
        for key, value in kwargs.items():
            setattr(self, key, PWM(Pin(value)))
            self.leds[key] = 255
    
    def display(self):
        if self.on:
            for key in self.leds:
                self.key.duty(self.leds.get(key)) # does not work
        else:
            for key in self.leds:
                 self.key.duty(0) # does not work either

Update 2 – Solved

Thank you guys for the answers. Using a dictionary instead of attributes solves the problem.

>Solution :

Since your colors are the keys of kwargs, which is a regular dictionary, you can do

self.color_list = list(kwargs.keys())

Then you can address them as

for color in self.color_list:
    getattr(self, color).duty(number_gained_via_mqtt)

If you just want a list of the PWM objects, you can do that too:

self.color_list = []
for color, pin in kwargs.items():
    pwm = PWM(Pin(pin))
    setattr(self, color, pwm)
    self.color_list.append(pwm)

Then you can do

for color in self.color_list:
    color.duty(number_gained_via_mqtt)

A better option might be to store everything into a dictionary rather than the instance __dict__, where all the attributes are mixed together:

self.colors = {color: PWM(Pin(pin)) for color, pin kwargs.items()}

Then you can access it much simpler:

for pin in self.colors.values():
    pin.duty(number_gained_via_mqtt)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading