标签:false span div before day return pytho cal style
带参数的装饰器
FLAG = False def log(flag): def wrapper(func): def inner(*args,**kwargs): if flag: print(‘call : %s‘%func.__name__) ret = func(*args,**kwargs) return ret return inner return wrapper @log(FLAG) #wrapper = log(FlAG) --> @wrapper --> qqxing = wrapper(qqxing) = inner def qqxing(): print(‘qqxing‘) qqxing() #inner()
此例子可以不改变装饰器的内容,仅通过改变全局变量来决定是否启用这个装饰器
装饰器嵌套
def wrapper1(func): def inner1(): print(‘wrapper1 ,before func‘) func() #f print(‘wrapper1 ,after func‘) return inner1 def wrapper2(func): def inner2(): print(‘wrapper2 ,before func‘) func() #inner1 print(‘wrapper2 ,after func‘) return inner2 @wrapper2 #f = wrapper2(wrapper1(f)) --> f = wrapper2(inner1) --> f = inner2 @wrapper1 def f(): print(‘in f‘) f()
标签:false span div before day return pytho cal style
原文地址:http://www.cnblogs.com/Amery313/p/7501984.html