I know the simplified example below is embarrassingly ugly ... hence why I have come to SO to share it with the world.
I would like to be able to call a class method from cls, specifying the particular method within callmethod.
class cls(object):
def __init__(self, var1):
self.var1 = var1
def method1(self):
return self.var1 ** 2
def method2(self):
return self.var1 ** 3
def callmethod(method, var1):
methods = {'method1' : cls(var1).method1(),
'method2' : cls(var1).method2()
}
return methods[method]
callmethod('method1', 2)
Out[56]: 4
Is there a way to go about this without creating a (possibly large and cumbersome) dict that links a string form of each method to the actual thing?
getattrwould negate the need for that dictionary, but I don't see how your real example and abstract example are connected at all.