I'm facing the following problem. I have a bunch of inputs and need to write a function that instantiates several objects (dataclass instances) that need a subset of these inputs.
So I've got something like:
def create_objects(**kwargs):
# as you can see some attrs are used in different objects.
obj1 = Object1(attr1=kwargs.get("attr1"), attr2=kwargs.get("attr2"))
obj2 = Object2(attr3=kwargs.get("attr3"), attr2=kwargs.get("attr2"))
Since all these dataclasses require multiple (between 2 and 10) attributes passed on to them, I would like to do some magic like, to check if the Object1 class has a certain property defined in the dataclass:
obj1 = Object1(**{k: v for k, v in kwargs.items() if hasattr(Object1, k)})
Obviously this doesn't work. Is there a way to do this pythonically?
Or is there a better approach?