thedarklord47 thedarklord47 - 1 year ago 57
Python Question

How to implement __setitem__ to set attributes of a class?

my classes

class Base:
#has no attributes of its own

def __init__(self, params):
for key in params:
if hasattr(self, key):
self[key] = params[key]

def __setitem__(self, key, value):
self[key] = value

class Child(Base):
prop1 = None
prop2 = None

This however, goes into endless recursion as
self[key] = value
recursively calls

My goal is to be able to pass a a dictionary into a
constructor like this

params = dict(
prop1 = "one",
prop2 = "two"
c = Child(params)

c.prop1 #"one"
c.prop2 #"two"

There are many different classes like Child but with different fields.
is a dict from a json blob. I want to use
as a generic populator for the different classes like

I have seen methods that use an internal
to accomplish what I am asking but it is my understand (I am brand new to Python) that this would prevent accessing the methods via dot notation (which I would rather avoid).

Answer Source

Just update the __dict__ of the instance in your __init__:

class Base:
    def __init__(self, params):
        for key in params:
            if hasattr(type(self), key):
                self.__dict__[key] = params[key]


class Child(Base):
    field1 = None
    field2 = None

c = Child(dict(field1="one", field2="two", field3="three"))

print(c.field1)     # "one"
print(c.field2)     # "two"
print(c.field3)     # => attr error

Grandchildren will behave:

class GrandChild(Child):
    field3 = None

gc = GrandChild(dict(field1="one", field2="two", field3="three"))

print(gc.field1)     # "one"
print(gc.field2)     # "two"
print(gc.field3)     # "three"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download