Im learning Python here so please spare me for silly questions. I Encounter an issue with adding attribute to a class instance
I have a dictionary of people with name,age and strength, i.e
{
"Mary": {"age":25, "strength": 80},
"John": {"age": 40, "strength": 70},
...
}
and a class that will get in list of people as constructor input and add them as its own attribute, and when that attribute is called, it will return the age
i.e:
group = Person({dictionary of person})
# call person name as attribute, get back age
first_person = group.Mary # return 25 here
group.John # return 40 here
However, each attribute will also need to maintain its behavior as a dict/object
group.Mary["strength"] # return 80 here
I tried __get()__
but it seems to work only on class variables which is not this case since I need to create multiple group instances of class Person and they don't share variables.
Also tried setattr()
but it will keep each attribute as a dict and therefore cannot directly call like group.Mary
to get age
May I know is there any way in Python to implement this requirement?