I am new to programming and I came across this piece of code in the following thread: Assign function arguments to `self`
class C(object):
def __init__(self, **kwargs):
self.__dict__ = dict(kwargs)
c = C(g="a",e="b",f="c")
print(c.g,c.e,c.f)
Output:
a b c
This would allow inputting any number of keyword arguments and assign them to attributes accordingly.
My questions are:
- Why does it work? What does
self.__dict__
do here? - Is there any other usage of
self.__dict__
?
I would also appreciate any kind of resources that could help me understand it. Thank you in advance.