标签:
普通装饰器:
def w1(func): def inner(): # 验证1 # 验证2 # 验证3 return func() return inner @w1 def f1(): print ‘f1‘
加参数装饰器:
def w1(func): def inner(*args,**kwargs): # 验证1 # 验证2 # 验证3 return func(*args,**kwargs) return inner @w1 def f1(arg1,arg2,arg3): print ‘f1‘
多层装饰器:
def w1(func): def inner(*args,**kwargs): # 验证1 # 验证2 # 验证3 return func(*args,**kwargs) return inner def w2(func): def inner(*args,**kwargs): # 验证1 # 验证2 # 验证3 return func(*args,**kwargs) return inner @w1 @w2 def f1(arg1,arg2,arg3): print ‘f1‘
前后函数通用装饰器:
#!/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‘
加元信息装饰器:
import functools def outer(func): @functools.wraps(func) def inner(*args, **kwargs): print(inner.__doc__) # None return func() return inner @outer def function(): """ asdfasd :return: """ print(‘func‘)
标签:
原文地址:http://www.cnblogs.com/goldd/p/5512782.html