完善decorator
import time, functools
def performance(unit):
def performance_decorator(f):
@functools.wraps(f)
def wrapper(*agrs,**kw):
t1=time.time()
r=f(*agrs,**kw)
t2=time.time()
t=(t2-t1)*1000 if unit=='ms' else (t2-t1)
print 'call %s() %s %s' % (f.__name__,t,unit)
return r
return wrapper
return performance_decorator
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(2)
print factorial.__name__
为什么在第二行会输出 2 这个结果呢