My title could be misleading. my question comes from this code snippet.
class myDecorator(object):
def __init__(self, f):
print "inside myDecorator.__init__()"
f() # Prove that function definition has completed
def __call__(self):
print "inside myDecorator.__call__()"
@myDecorator
def aFunction():
print "inside aFunction()"
print "Finished decorating aFunction()"
#aFunction()
when I execute, the above code, I get the output as
inside myDecorator.__init__()
inside aFunction()
Finished decorating aFunction()
however, I thought the output should only be
Finished decorating aFunction()
just decorating the function, calls the constructor and executes myDecorator object.I was under the impression that only when aFunction()
is called, the decorator pattern is executed. why is it so?
Another question about decorator:
This link explains the decorator as @ is just a little syntax sugar meaning "pass a function object through another function and assign the result to the original function.
what is the function object, another function and original function that this refers to?
The "function object" is
aFunction
. The "another function" ismyDecorator
. The "original function" should actually read "original function name", and would be "aFunction" in this case.