class Obj:
def __init__(self, **kw):
# code
obj = Obj(a=5, b=10)
print(obj.a, obj.b) # 5 10
Is there a proven solution to this task?
class Obj:
def __init__(self, **kw):
# code
obj = Obj(a=5, b=10)
print(obj.a, obj.b) # 5 10
Is there a proven solution to this task?
You can do the following to assign attributes passed in **kw
:
class Obj:
def __init__(self, **kw):
# in case of python 2, the following line is: for k, v in kw.iteritems():
for k, v in kw.items():
setattr(self, k, v)
and then use the way you mentioned in your post:
obj = Obj(a=5, b=10)
print(obj.a, obj.b) # 5 10