import types
from functools import wraps
class profiled:
def __init__(self,func):
wraps(func)(self)
def __call__(self,*a,**k):
print("profiled.call")
return self.__wrapped__(*a,**k)
def __get__(self,instance,cls):
if instance is None: return self
foo=types.MethodType(self,instance)
print("profiled.get:",foo)
return foo
class spam:
@profiled
def bar(self,x):print(x)
if name == 'main':
s=spam()
print(s.__dict__,spam.__dict__,'\n')
f=s.bar
print(f,type(f),f.__dict__,sep='\n')
f("TEST") #f 已经是绑定的方法了,为什么还要调用__call__()