本文實例講述了Python實現對一個函數應用多個裝飾器的方法。分享給大家供大家參考,具體如下:
下面的例子展示了對一個函數應用多個裝飾器,可以加多個斷點,在debug模式下,查看程序的運行軌跡。。。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/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() |
運行結果:
1
2
3
|
hello python 之前 hello python! hello python 之后 |
希望本文所述對大家Python程序設計有所幫助。
原文鏈接:http://blog.csdn.net/sxingming/article/details/52433019