class Classname(object):
def __init__(self, p1, p2=''):
self.p1 = p1
self.p2 = p2
# @Classname.decorator
def method_one(self, p_list):
return function_one(p_list)
def method_one(self, p_list):
return function_two(p_list)
def method_one(self, p_list):
return function_three(p_list)
A lot of them are like this method_one
calls funciton_one
, but now the call function_xxx
needs to be changed depending on whether the user passes in p2
The method, I hope to change it to something like this function_xxx
I cannot modify the code.
def method_two(self, p_list):
if self.p2:
return function_two(self.p2, p_list)
else:
return function_two(p_list)
I have considered using a decorator to handle it, but it seems that using a decorator can only wrap a layer around the function, but cannot intrusively change the calling method. Is there any suitable solution here?
If it is possible to modify the calling method based on self.p3 self.p4 in the future, is there any better solution?
You can use metaclasses to magically modify classes, here is an example