Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Decorator behavior

Reply
Thread Tools

Decorator behavior

 
 
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
Guest
Posts: n/a
 
      07-22-2011
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()
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      07-22-2011
On Fri, Jul 22, 2011 at 2:38 PM,
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
<> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      07-23-2011
On 01/-10/-28163 02:59 PM,
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why doesnt __getattr__ with decorator dont call __get_method in decorator glomde Python 5 03-29-2007 02:48 PM
layout decorator framework that fits well w/ spring William Z. Java 0 03-22-2006 11:19 PM
explanations about the Decorator design pattern Jean Lutrin Java 8 11-18-2004 05:40 PM
tweaking @decorator syntax Sandy Norton Python 14 08-06-2004 08:09 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM



Advertisments