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

栏目: 编程语言 · Python · 发布时间: 7年前

内容简介:这篇文章主要介绍了Python实现对一个函数应用多个装饰器的方法,结合实例形式分析了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程序设计有所帮助。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Google's PageRank and Beyond

Google's PageRank and Beyond

Amy N. Langville、Carl D. Meyer / Princeton University Press / 2006-7-23 / USD 57.50

Why doesn't your home page appear on the first page of search results, even when you query your own name? How do other web pages always appear at the top? What creates these powerful rankings? And how......一起来看看 《Google's PageRank and Beyond》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具