class Class1(object):
...
class Class2(object):
...
class Class3(object):
...
class A(object):
def _methA(parm1, parm2)
...
def _methB(parm1, parm2)
...
def _methC(parm1, parm2)
...
def manager(parm1, method, params)
...
if parm1.__class__.__name__==Class1.__name__:
response = _methA(parm1, params)
elif parm1.__class__.__name__==Class2.__name__:
response = _methB(parm1, params)
elif io_source.__class__.__name__==Class3.__name__:
response = _methC(parm1, params)
else:
raise Exception, "Unsupported parm1"
...
I didn't like the way that if/elif
block in manager()
looked and refactored it to this:
def manager(parm1, method, params)
...
try:
response = {
Class1.__name__: lambda parm1, parms: _methA(parm1, parms),
Class2.__name__: lambda parm1, parms: _methB(parm1, parms),
Class3.__name__: lambda parm1, parms: _methC(parm1, parms)
}[parm1.__class__.__name__](parm1, parms)
except KeyError:
raise Exception, "Unsupported parm1"
But the fact that the code is still looking at class names bothers me - I really don't know how to explain why... Should it bother me?
Is there a better way to write code to call a method in class A that, depending on the class of one of its parameters, triggers the calling of different methods in A?
PS. Sorry for the contrived example, but posting the actual code would make the question even more convoluted. I tried to distill the issue to its essence...