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

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

时间:2015-11-30 18:07:19      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:python   装饰器   python装饰器   

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

#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)---多装饰器的使用

标签:python   装饰器   python装饰器   

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

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