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

相关文章

Django ModelForm组件使用方法详解

Django ModelForm组件使用方法详解

一、创建ModelForm from django.forms import ModelForm from appxx import models from django.form...

python3 map函数和filter函数详解

map()函数可以对一个数据进行同等迭代操作。例如: def f(x): return x * x r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]...

django 实现编写控制登录和访问权限控制的中间件方法

django中,很多时候我们都需要有一个地方来进行更加详细的权限控制,例如说哪些用户可以访问哪些页面,检查登录状态等,这里的话就涉及到了中间件的编写了。 在django项目下的setti...

导入tensorflow:ImportError: libcublas.so.9.0 报错

导入tensorflow:ImportError: libcublas.so.9.0 报错

错误:ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory...

python利用matplotlib库绘制饼图的方法示例

python利用matplotlib库绘制饼图的方法示例

介绍 matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图。而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中。...