脚本专栏 
首页 > 脚本专栏 > 浏览文章

Python实现对一个函数应用多个装饰器的方法示例

(编辑:jimmy 日期: 2025/9/26 浏览:3 次 )

本文实例讲述了Python实现对一个函数应用多个装饰器的方法。分享给大家供大家参考,具体如下:

下面的例子展示了对一个函数应用多个装饰器,可以加多个断点,在debug模式下,查看程序的运行轨迹。。。

#!/usr/bin/env python
#coding:utf-8
def decorator1(func):
  def wrapper():
    print 'hello python 之前'
    func()
  return wrapper
def decorator2(func):
  def wrapper():
    func()
    print 'hello python 之后'
  return wrapper
@decorator1
@decorator2
def test():
  print 'hello python!'
test()

运行结果:

hello python 之前
hello python!
hello python 之后

关于python装饰器的更多介绍,可参考本站:

1. Python装饰器学习(九步入门)

2. Python装饰器与面向切面编程

更多关于Python相关内容可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

上一篇:Python with语句上下文管理器两种实现方法分析
下一篇:Python遍历pandas数据方法总结
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap