本文實例講述了Python實現對一個函數應用多個裝飾器的方法。分享給大家供大家參考,具體如下:
下面的例子展示了對一個函數應用多個裝飾器,可以加多個斷點,在debug模式下,查看程序的運行軌跡。。。
#!/usr/bin/env python#coding:utf-8def decorator1(func): def wrapper(): print 'hello python 之前' func() return wrapperdef decorator2(func): def wrapper(): func() print 'hello python 之后' return wrapper@decorator1@decorator2def test(): print 'hello python!'test()
運行結果:
hello python 之前hello python!hello python 之后
關于python裝飾器的更多介紹,可參考本站:
1. Python裝飾器學習(九步入門)
2. Python裝飾器與面向切面編程
更多關于Python相關內容可查看本站專題:《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答