本文實(shí)例講述了Python實(shí)現(xiàn)對(duì)一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器的方法。分享給大家供大家參考,具體如下:
下面的例子展示了對(duì)一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器,可以加多個(gè)斷點(diǎn),在debug模式下,查看程序的運(yùn)行軌跡。。。
#!/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()
運(yùn)行結(jié)果:
hello python 之前hello python!hello python 之后
關(guān)于python裝飾器的更多介紹,可參考本站:
1. Python裝飾器學(xué)習(xí)(九步入門)
2. Python裝飾器與面向切面編程
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注