I am unable to understand how decorating decorators works in Python (2.7.2). I have following piece of code:
def verbose(function):
print 'i am verbose and func is ' + function.__name__
def wrapper2(func):
print 'func is ' + repr(func)
result = function(func)
return result
return wrapper2
@verbose
def more(function):
print 'i am more and func is ' + function.__name__
def wrapper1(*args, **kwargs):
print 'args' + repr(args)
result = function(*args)
return result
return wrapper1
@more
def hello(*args):
print(sum(args))
when I run:
>>> hello(1,2,3)
this is what I get:
i am verbose and func is more
func is <function hello at 0x1015338c0>
i am more and func is hello
args(1, 2, 3)
6
I am unable to visualize the sequence of calls that generates this output. I think the following call will still generate the same output and I am keen to understand how decorated decorators work in this specific example.
>>> verbose(more)(hello)(1,2,3)
i am verbose and func is more
func is <function hello at 0x101533d70>
i am more and func is hello
args(1, 2, 3)
6