码迷,mamicode.com
首页 > 编程语言 > 详细

python装饰器

时间:2019-03-03 22:31:09      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:pre   result   嵌套   weight   hello   python   int   wrapper   app   

#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)

python装饰器

标签:pre   result   嵌套   weight   hello   python   int   wrapper   app   

原文地址:https://www.cnblogs.com/qinjiting/p/10467820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!