Python如何实现对一个函数应用多个装饰器
更新:HHH   时间:2023-1-7


这篇文章主要介绍了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如何实现对一个函数应用多个装饰器”这篇文章对大家有帮助,同时也希望大家多多支持天达云,关注天达云行业资讯频道,更多相关知识等着你来学习!

返回开发技术教程...