Decorator behavior

  • Thread starter mhearne808[insert-at-sign-here]gmail[insert-dot-he
  • Start date
M

mhearne808[insert-at-sign-here]gmail[insert-dot-he

I am just trying to wrap my head around decorators in Python, and I'm
confused about some behavior I'm seeing. Run the code below (slightly
adapted from a Bruce Eckel article), and I get the following output:

inside myDecorator.__init__()
inside aFunction()
Finished decorating aFunction()
inside myDecorator.__call__()

My question: Why isn't the first print statement in "__main__" the
first line of code executed? Is aFunction() not closed somehow?

#!/usr/bin/env python

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()"

if __name__ == '__main__':
print "Finished decorating aFunction()"
aFunction()
 
I

Ian Kelly

I am just trying to wrap my head around decorators in Python, and I'm
confused about some behavior I'm seeing.  Run the code below (slightly
adapted from a Bruce Eckel article), and I get the following output:

inside myDecorator.__init__()
inside aFunction()
Finished decorating aFunction()
inside myDecorator.__call__()

My question: Why isn't the first print statement in "__main__" the
first line of code executed?  Is aFunction() not closed somehow?

Because everything in the module is executed in order. First the
myDecorator class is defined. Then the aFunction function is defined
and the decorator is applied to it (which involves calling the
decorator). Finally the if condition is tested, and if it's true, the
"Finished decorating" string is printed and the decorated function is
called.

If this module were not the main module, the exact same thing would
happen, except that the if would evaluate false, and so that part of
the code would be skipped.

By the way, your email address is not mangled. The label part looks
like a mangled email, but the actual address part is intact.
 
D

Dave Angel

I am just trying to wrap my head around decorators in Python, and I'm
confused about some behavior I'm seeing. Run the code below (slightly
adapted from a Bruce Eckel article), and I get the following output:

inside myDecorator.__init__()
inside aFunction()
Finished decorating aFunction()
inside myDecorator.__call__()

My question: Why isn't the first print statement in "__main__" the
first line of code executed? Is aFunction() not closed somehow?

#!/usr/bin/env python

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()"

if __name__ == '__main__':
print "Finished decorating aFunction()"
aFunction()
classes and functions and decorators have some portions that execute
when they occur, long before anybody "calls" them. (I'm sure there are
other examples; one might consider imports the same way)

In the case of classes, anything outside of the method definitions will
happen before the class definition is completed. For example, class
attributes happen at that time.

For functions/methods, default arguments are evaluated at the definition
time. So if the default value makes a call, the call will happen at
that time.

Function decorators execute right after the corresponding function
definition is built. Such decorators won't normally call the function,
but as you notice, if you do call it, it will execute.

When you think about it, these behaviors are the only reasonable way
these things could be done, unless the compiler tried to do some "just
in time" compiling, not really building the code till somebody uses it.
And that would make the language a lot different.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top