Directory (?) [+]
function target
Write an adorner that can print the decorated function name, execution time, memory address
Predecessor Dependency Package
Import time
Import Functools
From decorator import Decorator
Based on common function nesting
def log1(fn): def _wrapper(*args, **kwargs): start = time.clock() result = fn(*args, **kwargs) print("%s is invoked with time consumed: %s seconds at address %s" % (fn.__name__, str(time.time() - start), id(fn))) return result return _wrapper
Based on @decorator
@decoratordef log(f, *args, **kwargs): start = time.time() result = f(*args, **kwargs) print("%s is invoked with time consumed: %s at address %s" % (f.__name__, str(time.time() - start), id(f))) return result
Using @functools
def log2(f): @functools.wraps(f) def wrapper(*args, **kwargs): start = time.clock() result = f(*args, **kwargs) print("%s is invoked with time consumed: %s seconds at address %s" % (f.__name__, str(time.time() - start), id(f))) return result return wrapper
Test
@log2def f11(x, y): return x**yresult = f11(2,3)print("result:" + str(result))
Switching to a different implementation, their effect is the same.
http://www.woaipu.com/shops/zuzhuan/61406
Http://www.znds.com/tv-967956-1-1.html
Http://www.znds.com/tv-967958-1-1.html
Three ways to implement decorative patterns in Python