Menu
  • HOME
  • TAGS

Is there any way to get class instance attributes without creating class instance?

python,dictionary,class-instance-variables

If you insist on using an overly general dictionary to initialize your object, just define __init__ to accept, but ignore, the extra keys. class MyClass(object): def __init__(self, attr1, attr2, **kwargs): self.attr1 = attr1 self.attr2 = attr2 dict = {'attr1': 'value1', 'attr2': 'value2', 'extra_key': 'value3'} new_instance = MyClass(**dict) If you can't...