Python中的多重装饰器

770次阅读  |  发布于5年以前

多重装饰器,即多个装饰器修饰同一个对象【实际上并非完全如此,且看下文详解】

1.装饰器无参数:

复制代码 代码如下:

def first(func):
print '%s() was post to first()'%func.func_name
def _first(*args,*kw):
print 'Call the function %s() in _first().'%func.func_name
return func(
args,**kw)
return _first

def second(func):
print '%s() was post to second()'%func.func_name
def _second(*args,*kw):
print 'Call the function %s() in _second().'%func.func_name
return func(
args,**kw)
return _second

@first
@second
def test():return 'hello world'

test() was post to second()
_second() was post to first()

test()
Call the function _second() in _first().
Call the function test() in _second().
'hello world'

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8