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

yipeiwu_com6年前Python基础

本文实例讲述了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中用print()输出多个格式化参数的方法

不废话,直接贴代码: disroot = math.sqrt(deta) root1 = (-b + disroot)/(2*a) root2 = (-b - disroot)/(2...

python中常用的各种数据库操作模块和连接实例

工作中,经常会有用python访问各种数据库的需求,比如从oracle读点配置文件或者往mysql写点结果信息之类的。这里列一下可能用到的各个模块。 sqlite3: 内置模块用sqli...

Python常用随机数与随机字符串方法实例

随机整数: 复制代码 代码如下: >>> import random >>> random.randint(0,99) 21 随机选取0到100间的...

python Tkinter版学生管理系统

python Tkinter版学生管理系统

本文实例为大家分享了python Tkinter版学生管理的具体代码,供大家参考,具体内容如下 Tkinter是python自带的UI包,无需下载,只需要导入 tkinter 文档 //...

Scrapy框架CrawlSpiders的介绍以及使用详解

Scrapy框架CrawlSpiders的介绍以及使用详解

在Scrapy基础——Spider中,我简要地说了一下Spider类。Spider基本上能做很多事情了,但是如果你想爬取知乎或者是简书全站的话,你可能需要一个更强大的武器。CrawlSp...