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


Groups > comp.lang.python > #10154

Re: Decorator behavior

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.wiretrip.org!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'happen,': 0.07; 'called.': 0.09; 'decorator': 0.09; 'defined.': 0.09; 'executed': 0.09; 'false,': 0.09; 'question:': 0.09; 'defined': 0.14; 'wrote:': 0.15; '2:38': 0.16; 'mangled': 0.16; 'skipped.': 0.16; 'subject:behavior': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'true,': 0.19; 'wrap': 0.19; '(which': 0.20; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply- To:1': 0.22; 'trying': 0.23; 'module,': 0.23; 'code': 0.24; 'bruce': 0.25; 'statement': 0.25; 'string': 0.26; 'function': 0.26; "i'm": 0.27; 'fri,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'order.': 0.29; 'cc:addr:python.org': 0.30; 'module': 0.30; '22,': 0.30; 'confused': 0.30; 'decorators': 0.30; 'looks': 0.30; 'class': 0.31; 'print': 0.32; 'calling': 0.34; "isn't": 0.35; 'actual': 0.35; 'some': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85.161': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'finished': 0.39; 'except': 0.39; 'received:209': 0.40; 'your': 0.60; 'address': 0.61; 'closed': 0.62; 'below': 0.62; 'exact': 0.69; 'evaluate': 0.71
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=DRZmA4IOwF32A0tEhc8GAqvc0CcbUFD2ojHc6mQAawQ=; b=tDbhuZBH1/qXFai70ZI8ZG0uX33XZOWIBW0FkT5Qfh97wQ2pEQMjAwjEQy3h+PShAH ad9G9OXoccEzw5LK6RG8BQXHqDqFBiN5RMo+fZa2WWQAmWdyb08l38Gln49PRvUa4i+B vZ9cBHxv3sdfPimZ4w14WtFpztAieIvYXkABk=
MIME-Version 1.0
In-Reply-To <8a7d158b-bc27-442c-9886-1a704d0c52e1@a11g2000yqm.googlegroups.com>
References <8a7d158b-bc27-442c-9886-1a704d0c52e1@a11g2000yqm.googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 22 Jul 2011 14:59:50 -0600
Subject Re: Decorator behavior
To "mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" <mhearne808@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1391.1311368422.1164.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311368422 news.xs4all.nl 23864 [2001:888:2000:d::a6]:56448
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10154

Show key headers only | 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