這篇文章給大家分享的是有關(guān)python中嵌套函數(shù)的裝飾器有什么用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。
如何在函數(shù)是嵌套函數(shù),需要函數(shù)傳入的參數(shù),在內(nèi)部函數(shù)依然可以使用,經(jīng)常能夠看到這個(gè)問題,下面給大家準(zhǔn)備好了一段代碼,而裝飾器是下面的my_decorator。
具體內(nèi)容如下:
def my_decorator(func): def wrapper(): print('wrapper of decorator') func() return wrapper def greet(): print('hello world') greet = my_decorator(greet) greet() # 輸出 wrapper of decorator hello world
感謝各位的閱讀!關(guān)于python中嵌套函數(shù)的裝飾器有什么用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!