Home > OS >  Setting class attributes/properties from a dict holding properties
Setting class attributes/properties from a dict holding properties

Time:10-31

I have a class with some attributes/properties that are to be set at init by reading a dictionary with str as key and item. These values are going to be turned into class properties/attributes. so I have something like this:

class Attendee:
    def __init__(self, attendee):
        # creating a small example attendee dict here:
        self.attendee = {"email": "[email protected]", "id": "1234"}
        self._email = None
        self._id = None

        self.init_attribute = {"email": self.email, "id": self.id}

        self.init_attributes()

    def init_attributes(self):
        for k in self.attendee.keys():
            self.init_attribute[k] = self.attendee[k]

    @property
    def email(self):
        return self._email

    @email.setter
    def email(self, value):
        self._email = value

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, value):
        self._id = value


The idea is that using the dict self.init_attribute is going to save me a lot of lines of code. However, this does not work. All attributes are still None after init. Thus, self.init_attribute[k] = self.attendee[k] does not manage to set the attributes when called like this. Can anyone explain to me why this is not working, and a possible solution?

CodePudding user response:

The function init_attributes is not setting the attributes of your class. One approach is to use setattr:

def init_attributes(self):
    for k, v in self.attendee.items():
        setattr(self, k, v)

then it can be done as follows:

att = Attendee(None)
print(att.email)

Output

[email protected]

CodePudding user response:

you dont need setter functions . please try the below solution

class Attendee:
    def __init__(self, attendee):
        # creating a small example attendee dict here:
        self.attendee = attendee
        self._email = self.attendee['email']
        self._id = self.attendee['id']

    @property
    def email(self):
        return self._email

    @email.setter
    def email(self, value):
        self._email = value

    @property
    def id(self):
        return self._id

    @id.setter
    def id(self, value):
        self._id = value
a = Attendee({"email": "[email protected]", "id": "1234"})
print(a.email)
print(a.id)

CodePudding user response:

So the fundamental problem here is that you're never actually setting the value of the properties to anything; in init_attributes, you're just setting the value of a given key to the value that that key has in a different dict, but this is never actually assigned to the attribute.

To achieve what you're trying to do, you could us the operator module as follows to deconstruct your dict and assign it directly to the relevant attributes:

from operator import itemgetter

class Attendee:
def __init__(self, attendee):
    # creating a small example attendee dict here:
    self.attendee = {"email": "[email protected]", "id": "1234"}
    self._email, self._id = itemgetter('email', 'id')(self.attendee)
  • Related