class tracer(object):
def __init__(self, func):
self.calls = 0
self.func = func
def __call__(self, *args, **kwargs):
self.calls += 1
print('call %s to %s' % (self.calls, self.func.__name__))
return self.func(*args, **kwargs)
def __get__(self, instance, owner):
def wrapper(*args, **kwargs):
print('wrapper:args=%s ,kwargs=%s'% (args,kwargs))
return self(instance, *args, **kwargs)
return wrapper
class Person:
def __init__(self, name, pay):
self.name = name
self.pay = pay
@tracer
def giveRaise(self, percent):
self.pay *= (1.0+percent)
print("pay=",self.pay)
bob = Person('Bob Smith', 50000)
bob.giveRaise(.10)
上面代码的输出如下:
wrapper:args=(0.1,) ,kwargs={}
call 1 to giveRaise
pay= 55000.00000000001
我的问题是:wrapper 方法是在__get__方法内定义的,但是__get__方法并没有接收参数(*args, **kwargs),为何 wrapper 方法的(*args, **kwargs)参数会有值呢?