首页 > 编程语言 > 详细

python学习系列之python装饰器基础(5)---多装饰器的使用

时间:2015-11-30 18:04:29      阅读:247      评论:0      收藏:0      [点我收藏+]

有些时候,可能实际工作中需要同时使用多个装饰器,具体怎么用,见代码:

#basic5.py

def auth1(func):
    def inner():
        print ‘before 1‘
        func()
        print ‘after 1‘
    return inner


def auth2(func):
    def inner():
        print ‘before 2‘
        func()
        print ‘after 2‘
    return inner


@auth2
@auth1
def f1():
    print ‘f1‘

执行部分:

#b5.py

#coding:utf-8

import basic5

basic5.f1()

执行结果:

#python b5.py
before 2
before 1
f1
after 1
after 2


本文出自 “苦咖啡's运维之路” 博客,请务必保留此出处http://alsww.blog.51cto.com/2001924/1718270

python学习系列之python装饰器基础(5)---多装饰器的使用

原文:http://alsww.blog.51cto.com/2001924/1718270

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!