Basically, I have a list of classes which take in the same input parameters (either ClassX(a, **kwargs)
or ClassX(**kwargs)
or ClassX(b, **kwargs)
. I want to create a list of instantiated classes. Right now I sequentially go through each for creation.
Is there a way to use list comprehension effectively to make this cleaner? For example perhaps making a list of class objects and annotating those that require formal arguments (a
and b
) in addition to **kwargs
? I.e. looping over something like:
[ClassA: (None, None), ClassB: (fnA, None), ClassC: (None, fnB)]
to achieve the creation of:
listOfClasses.append(ClassA(**kwargs))
listOfClasses.append(ClassB(**kwargs, a=fnA))
listOfClasses.append(ClassC(**kwargs, b=fnB))
The reason I ask is because in my case I have 20-30 different classes that need to be created and appended to the list sequentially.
To illustrate better, the exact code I am using now is the following:
def method(self, **kwargs):
chain = []
chain.append(ClassA(**kwargs))
chain.append(ClassB(**kwargs))
chain.append(ClassC(a=static_method_x, **kwargs))
chain.append(ClassD(b=static_method_y, **kwargs))
chain.append(ClassE(**kwargs))
chain.append(ClassF(a=static_method_z, **kwargs))
...
So every instantiation follows one of three types, but **kwargs
is always the same. The values of a
or b
when they are classes which require them, however, differ, and correspond to other static methods in my class. I am wondering if there is a way to make this cleaner through list comprehension with a predefined list of sequential classes and respective arguments.