Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10154

Re: Decorator behavior

References <8a7d158b-bc27-442c-9886-1a704d0c52e1@a11g2000yqm.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-07-22 14:59 -0600
Subject Re: Decorator behavior
Newsgroups comp.lang.python
Message-ID <mailman.1391.1311368422.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jul 22, 2011 at 2:38 PM,
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
<mhearne808@gmail.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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Decorator behavior "mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808@gmail.com> - 2011-07-22 13:38 -0700
  Re: Decorator behavior Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-22 14:59 -0600
  Re: Decorator behavior Dave Angel <davea@ieee.org> - 2011-07-22 20:05 -0400

csiph-web