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?
學(xué)習(xí)是最好的投資!
You can use metaclasses to magically modify classes, here is an example
def function_one(*args):
print(1, args)
def function_two(*args):
print(2, args)
def make_method(func):
# 此處填邏輯
def _method(self, plist):
func(plist)
return _method
# 元類工廠方法,傳入包含 function_xxx 的模塊對(duì)象
def meta(mod):
class Meta(type):
def __new__(cls, name, bases, attrs):
fnames = attrs.get('FUNCTIONS', [])
for n in fnames:
func = getattr(mod, 'function_' + n)
attrs['method_'+n] = make_method(func)
return super(Meta, cls).__new__(cls, name, bases, attrs)
return Meta
import sys
myself = sys.modules[__name__]
class Class(metaclass=meta(myself)):
FUNCTIONS = ['one', 'two']
obj = Class()
obj.method_one('one')
obj.method_two('two')