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

python装饰器

时间:2018-09-26 13:05:12      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:python装饰器   bubuko   int   info   pytho   png   image   com   wrapper   

技术分享图片

 

带参数的装饰器

最多迭代三层函数

技术分享图片

 

多个装饰器装饰同一个函数

先使用最近的装饰器

先使用@wrapper1,再使用@wrapper2

def wrapper1(func):
    def inner():
        print(wrapper1 ,before func)
        func()
        print(wrapper1 ,after func)
    return inner

def wrapper2(func):
    def inner():
        print(wrapper2 ,before func)
        func()
        print(wrapper2 ,after func)
    return inner

@wrapper2
@wrapper1
def f():
    print(in f)

f()

wrapper2 ,before func
wrapper1 ,before func
in f
wrapper1 ,after func
wrapper2 ,after func

技术分享图片

技术分享图片

 

python装饰器

标签:python装饰器   bubuko   int   info   pytho   png   image   com   wrapper   

原文地址:https://www.cnblogs.com/daoyueweiku/p/9670436.html

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