原始class方法修饰后的使用需要对象实例作为参数

Usage of the original class method after being decorated requires the object instance as parameter

我有以下代码: 一个装饰者:

def pyDecorator(func):
    print func
    @wraps(func)
    def wrapped(*args, **kwargs):
        print args
        print kwargs
        tBegin = time()
        result = func(*args, **kwargs)
        tEnd = time()
        if result:
            # UI update
            print("\nTBegin '{}'({} s)".format(func.__name__,  tBegin))
            # UI and report update
            print("TEnd '{}' ({} s) ({} s) Result:{}".format(func.__name__, tEnd,tEnd - tBegin, result))  
        return result
    #workarround to use the original function
    wrapped._original=func 
    return wrapped 

和修饰的 class 方法:

class Dummy(object): 
    @pyDecorator
    def ClassMethod(self):
        print "Original class code executed"
        return True

如果我按以下方式调用原始函数的方法,我会收到此错误 "TypeError: ClassMethod() takes exactly 1 argument (0 given):"

ClassInstance.ClassMethod._original()

所以我不得不使用以下调用:

ClassInstance.ClassMethod._original(ClassInstance)

是否可以像第一种方式那样做?我不明白为什么我应该把 class 实例作为参数,因为它已经提供了。

ClassInstance.ClassMethod._original 是一个未绑定到任何 class 实例的函数。

请注意,当通过 class 实例(例如,使用点引用)访问函数对象时,会发生从函数到方法的转换。然而,在这里,_original 仅绑定到另一个函数对象 wrapper(在运行时提升为绑定方法)而不是 class 实例。因此不传递隐式 self 参数。你必须明确地传递它。

ClassInstance.ClassMethod._original
^
|- instance   ^           
              |- method
                          ^
                          |- function object bound to method

I do not understand why I should put the class instance as a parameter when it is already provided

不,尚未提供。