My primary goal was to have a dictionary to select method to run. It was easy to do per this suggestion. But now I am somehow blocked on using the inheritance concept to call child overridden method.
class A():
def sum(self, id: int):
return 1
def multiply(self, id: int):
return 2
def process(self, type: str, id: int):
callable_method = self.__dispatcher.get(type)
return callable_method(self, id) # Runs
#callable_method(id) # Doesnt work with my object, says parameter mismatch
#self.callable_method(id) # Doesn't Work obviously as there is no callable method in self
__dispatcher = { "+": sum, "*": multiply }
class B(A):
def process(self, type: str, id: int):
return super(B, self).process(type, id)
def multiply(self, id: int):
return 3
# main class call:
ob = B()
ob.process("*", 0) # This is returning 2 instead of 3
The above overriding works perfectly well if I dont use the dictionary and method references and directly use the method in parent's process() like self.multiply(id)
I might have an idea why this is not working but is there a way to make this work in Python?
Note:
- Dont want to use exec() , eval() due to security issues
- I'm not trying to write a calculator, actual problem is related to software design
- using Python 3.8