Here is the link to my first question: Creating class instance from dictionary?
So I am trying to create class instance from dictionary which holds keys that class has not. For example:
class MyClass(object):
def __init__(self, value1, value2):
self.attr1 = value1
self.attr2 = value2
dict = {'attr1': 'value1', 'attr2': 'value2', 'redundant_key': 'value3'}
Before creating class instance I have to delete this redundant_key
from dict
dict.pop('redundant_key', None)
new_instance = MyClass(**dict)
The problem is that I have several classes and several dicts with a lot of keys (I have several responses in json format which are represented as dicts and I want to create objects from this json responses). I already found an interim solution from previous question - how to delete redundant keys. I can create new dict from old dict only with keys that I need:
new_dict = {key: old_dict[key] for key in allowed_keys}
So here is the code:
class MyClass(object):
def __init__(self, value1, value2):
self.attr1 = value1
self.attr2 = value2
dict = {'attr1': 'value1', 'attr2': 'value2', 'redundant_key': 'value3'}
new_instance = MyClass(**{key: dict[key] for key in allowed_keys})
All I need now is to get allowed_keys
. So the question - Is there any way to get class instance attributes without creating class instance?
allowed_keys
defined? – Tilerattr1
andattr2
inallowed_keys
whenattr1
,attr2
are class instance' attributes – Sandalwood__init__
method – Sandalwood__init__
will create, but its argument signature: The arguments it accepts. That's a different problem, and it has a solution :-) – Diarrhea