装饰器:
def wrapper(func): if login('kk'): return func def login(user): if user == 'kk': return True else: print "invalid username" def readirct(url): pass @wrapper def home(): print "show home page!" home()
简单装饰器语法:
def
wrapper(func):
def
result():
print
'before'
func()
print
'after'
return
result
@wrapper #语法调用装饰器,把foo函数以参数形式传进wrapper函数里
def
foo():
print
'foo'
foo()
结果是:
before
foo
after
import functools def wrapper(func): @functools.wraps(func) def wrapper(): print 'before' func() print 'after' return wrapper @wrapper def foo(): print 'foo'
#!/usr/bin/env python #coding:utf-8 def Before(request,kargs): print 'before' def After(request,kargs): print 'after' def Filter(before_func,after_func): def outer(main_func): def wrapper(request,kargs): before_result = before_func(request,kargs) if(before_result != None): return before_result; main_result = main_func(request,kargs) if(main_result != None): return main_result; after_result = after_func(request,kargs) if(after_result != None): return after_result; return wrapper return outer @Filter(Before, After) def Index(request,kargs): print 'index' if __name__ == '__main__': Index(1,2)
装饰器传动态参数:
def auth(func): def inner(*args,**kwargs): print 'before' func(*args,**kwargs) print 'after' return inner @auth def a1(): print 'a1' @auth def a2(k): print 'a2 + %s' % k @auth def a3(r,t): print 'a3 +%s+%s' % (r,t)
结果:
before
a1
after
before
a2 + ddd
after
before
a3 +ss+gg
after
被装饰函数里有返回值:
1 def auth(func): 2 def inner(*args,**kwargs): 3 print 'before' 4 temp = func(*args,**kwargs) 5 print 'after' 6 return temp 7 return inner 8 9 @auth 10 def server_list(arg): 11 print '%s a' % arg 12 s_list = ['a1','a2','a3'] 13 return s_list 14 15 ret = server_list('test') 16 print ret
结果:
before
test a
after
['a1', 'a2', 'a3']
得到了返回值ret
运用装饰器写的简单用户登陆:
1 def login(): 2 name = 'cba' 3 if name == 'nba': 4 return True 5 else: 6 return False 7 8 9 def auth(func): 10 def inner(*args,**kwargs): 11 is_login = login() 12 if not is_login: 13 return u'非法用户!' 14 print 'before' 15 temp = func(*args,**kwargs) 16 print 'after' 17 return temp 18 return inner 19 20 @auth 21 def server_list(arg): 22 print '%s a' % arg 23 s_list = ['a1','a2','a3'] 24 return s_list 25 26 @auth 27 def a3(r,t): 28 print 'a3 +%s+%s' % (r,t) 29 30 ret1 = a3('sss','kkk') 31 print ret1 32 33 print '=============================================' 34 ret = server_list('test') 35 print ret
结果:
非法用户!
=============================================
非法用户!
简单传key或token的登陆验证:
1 def login(token): 2 loca = 'sdjkjisldkasld.,sdijkosdjhjshdjaa%' 3 if loca == token: 4 return True 5 else: 6 return False 7 8 9 def auth(func): 10 def inner(*args,**kwargs): 11 key = kwargs.pop("token") #这里pop把字典中key为token的值获取,并把该token参数去掉,不会影响下面原生func函数传参错误 12 is_login = login(key) 13 if not is_login: 14 return u'非法用户!' 15 print 'before' 16 temp = func(*args,**kwargs) 17 print 'after' 18 return temp 19 return inner 20 21 @auth 22 def server_list(arg): 23 print '%s a' % arg 24 s_list = ['a1','a2','a3'] 25 return s_list 26 27 28 key = 'sdjkjisldkasld.,sdijkosdjhjshdjaa%' 29 print '=============================================' 30 ret = server_list('test',token=key) 31 print ret
结果:
=============================================
before
test a
after
['a1', 'a2', 'a3']
多个装饰器:
1 def w1(func): 2 def inner(): 3 print 'w1 before' 4 func() 5 print 'w1 after' 6 return inner 7 8 def w2(func): 9 def inner(): 10 print 'w2 before' 11 func() 12 print 'w2 after' 13 return inner 14 15 @w1 16 @w2 17 def foo(): 18 print 'foo' 19 20 foo()
结果:
w1 before
w2 before
foo
w2 after
w1 after
它的原理相当于,刚开始有个盒子foo,然后外面套上一层w2,再套一层w1,要看foo则需要从外面w1往里一层层拨开,从代码看也是从上往下执行!
装饰器加参数:
1 #!/usr/bin/env python 2 #coding:utf-8 3 4 def Before(request,kargs): 5 print 'before' 6 7 def After(request,kargs): 8 print 'after' 9 10 11 def Filter(before_func,after_func): 12 def outer(main_func): 13 def wrapper(request,kargs): 14 15 before_result = before_func(request,kargs) 16 if(before_result != None): 17 return before_result; 18 19 main_result = main_func(request,kargs) 20 if(main_result != None): 21 return main_result; 22 23 after_result = after_func(request,kargs) 24 if(after_result != None): 25 return after_result; 26 27 return wrapper 28 return outer 29 30 @Filter(Before, After) 31 def Index(request,kargs): 32 print 'index'
Index(11,22)
结果:
before
index
after
后续。。