我试图装饰一个类并覆盖其方法之一的行为。但是在Python2.7中,我遇到了缺少的位置参数错误:
def supress_help(cls, a, b):
def wrapper(func):
def wrappy(*arg, **kwargs):
if arg[1] < 5:
func(arg[1])
else:
print("Not calling the helper")
return wrappy
cls.help = wrapper(cls.help)
print(a + b)
return cls
@supress_help(a=7, b="Hello")
class Helper(object):
def help(x):
print(x)
if __name__ == "__main__":
obj = Helper()
obj.help(2)
obj.help(7)
Traceback (most recent call last):
File "/home/aditya/class_decorator.py", line 14, in <module>
@supress_help(a=7, b="Hello")
TypeError: supress_help() takes at least 1 argument (2 given)