I have following structure for class.
class foo(object):
def __class__(self,param1):
pass
class bar(object):
def __class__(self,param1,param2):
pass
I have many classes of this type. And i am using this callable class as follows.
classes = [foo(), bar()]
for C in classes:
res = C(param1)
'''here i want to put condition if class takes 1 argumnet just pass 1
parameter otherwise pass two.'''
I have think of one pattern like this.
classes = [foo(), bar()]
for C in classes:
if C.__class__.__name__ in ['bar']:
res = C(param1, param2)
else:
res = C(param2)
but in above solution have to maintain list of class which takes two arguments and as i will add more class to file this will become messy.
I dont know whether this is correct(pythonic) way to do it.
- On more idea i have in mind is to check how many argument that class is taking. If its 2 then pass an additional argument otherwise pass 1 argument.
- Few things about this:
- There are only two type of classes in my usecase one with 1 argument and one with 2.
- Both class takes first argument same so
params1
in both case is same argument i am passing. in case of class with two required parameter i am passing additional argument(params2
) containing some data. Ps : Any help or new idea for this problem are appretiated.
Aucun commentaire:
Enregistrer un commentaire