网络编程
位置:首页>> 网络编程>> Python编程>> Python实现对一个函数应用多个装饰器的方法示例

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

作者:快递小可  发布时间:2022-01-23 17:30:22 

标签: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程序设计有所帮助。

来源:http://blog.csdn.net/sxingming/article/details/52433019

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com