python装饰器

#coding=utf-8

def outer(fun):
def wrapper():
print ('验证')

fun()

print ('验证成功')
return wrapper

@outer
def func1():
print ('func1')

@outer
def func2():
print ('func2')

@outer
def func3():
print ('func3')

@outer
def func4():
print ('func4')


def before_func(request,kargs):
print ('myname is before func,request is %s,kargs is %s'%(request,kargs))

def main_func(request,kargs):
print ('myname is main func,request is %s,kargs is %s'%(request,kargs))


def after_func(request,kargs):
print ('myname is after func,request is %s,kargs is %s'%(request,kargs))


#装饰器的层层嵌套
def fiter(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


@fiter(before_func,after_func)
def test(request,args):
print ('running...')

if __name__=="__main__":

# func1()
#
# func2()
#
# func3()
#
# func4()

s=test('hello','word')

print (s)
posted @ 2019-03-03 21:38  你居然是华神  阅读(195)  评论(0编辑  收藏  举报