标签:属性 装饰器 call tools line 情况 ref tool too
import time def performance(f): def fn(*args,**kw): t1 = time.time() r=f(*args,**kw) t2 = time.time() print ‘call %s() in %fs‘%(f.__name__,(t2-t1)) return r return fn @performance def factorial(n): return reduce(lambda x,y: x*y, range(1, n+1)) print factorial(10)
原理:
def log(prefix): def log_decorator(f): def wrapper(*args, **kw): print ‘[%s] %s()...‘ % (prefix, f.__name__) return f(*args, **kw) return wrapper return log_decorator @log(‘DEBUG‘) def test(): pass print test()
实例代码:
上一节的@performance只能打印秒,请给 @performace 增加一个参数,允许传入‘s‘或‘ms‘:
import time def performance(unit): def unit_decorator(f): def wrapper(*args,**kw): t1 = time.time() r=f(*args,**kw) t2 = time.time() print ‘call %s() in %fs %s‘% (f.__name__,(t2-t1),unit) return r return wrapper return unit_decorator @performance(‘ms‘) def factorial(n): return reduce(lambda x,y: x*y, range(1, n+1)) print factorial(10)
原理:
import functools def log(f): @functools.wraps(f) def wrapper(*args, **kw): print ‘call...‘ return f(*args, **kw) return wrapper
实例:
请思考带参数的@decorator,@functools.wraps应该放置在哪:
@functools.wraps(f) 这句代码应该放置在函数 f 定义之后
import time, functools def performance(unit): def perf_decorator(f): @functools.wraps(f) def wrapper(*args,**kw): t1 = time.time() r=f(*args,**kw) t2 = time.time() print ‘call %s() in %fs %s‘% (f.__name__,(t2-t1),unit) return r return wrapper return perf_decorator @performance(‘ms‘) def factorial(n): return reduce(lambda x,y: x*y, range(1, n+1)) print factorial.__name__
functools.partial能够减少函数的参数,少的参数需要在创建时指定默认值。
import functools int2 = functools.partial(int, base=2) int2(‘1000000‘) 例子:
sorted(iterable, cmp = None, key = None, reverse = False),返回值为一个新的列表list。
可以在sorted 的cmp参数位置传入自定义排序函数,用functools.partial可以把它简化。
代码1:
import functools sorted_ignore_case = functools.partial(sorted, cmp=lambda s1, s2: cmp(s1.upper(), s2.upper())) print sorted_ignore_case([‘bob‘, ‘about‘, ‘Zoo‘, ‘Credit‘])
(代码中s1.upper()都换成lower()也可以)
代码2:
import functools sorted_ignore_case = functools.partial(sorted, key = str.lower) print sorted_ignore_case([‘bob‘, ‘about‘, ‘Zoo‘, ‘Credit‘])
标签:属性 装饰器 call tools line 情况 ref tool too
原文地址:http://www.cnblogs.com/oucsheep/p/6711464.html