Need help with a little problem here. I want to create instance attributes from the input to a class __init__
method.
class Test():
def __init__(self, *args, **kwargs):
"""Create instance attributes from dict"""
t = Test(a=1, b=2, c=3)
print(t.a, t.b, t.c)
kwargs
is a dictionary and you can set class attributes from a dictionary using self.__dict__
:
class Test:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
t = Test(a=1, b=2, c=3)
print(t.a, t.b, t.c)
Output:
1 2 3