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


Groups > comp.lang.python > #36855

Re: i can't understand decorator

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'output': 0.04; 'wrapped': 0.07; 'called.': 0.09; 'effect.': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'output?': 0.16; 'twice.': 0.16; 'wrote:': 0.17; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'lines': 0.28; '>>>>': 0.29; 'cases.': 0.29; 'function': 0.30; 'expect': 0.31; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'two': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'called': 0.39; 'header:Received:5': 0.40; "you'll": 0.62; '2013': 0.84; 'oscar': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=EaieTiRfGSXauVop2zal32taOWi3aiaqQ/i9jQrij8I=; b=gDmw2AQN9b6y0BoswMQh6zXPLzvDOoZz9M8Ihs262bIUu3zBCGsleAxEGvlvUzDv15 2pq5MJNv8StGBm85UoS68a5eqkJeqcLBCWVwR9LzZ0Zq0Mulwbn0QgK84cu5EbXMWOwB F3EXbJ5GuV0YnjpOpBMIa6yFTCcYZbkzhs9dnZjxjrCmes0gDvdbDxzOfee0mk2++y3v EfkWqms1FxYKFmscF5mWNQ+Psq0GwGKSCpCOsifjPD8QnNct0rP3sg5bGPUQ+tw/sxYd A3Ub81Q/qt7QEcrctO1cZrTMnXpSZyNqguB6QvK2WwO17MvgsrZD0rUMJtL+RRpeoqGo fIsg==
MIME-Version 1.0
In-Reply-To <CA+YdQ_6YMfM1_bsPntYgCafG-0R_ocMQpgX_Ez3p4Z1pg9LjVw@mail.gmail.com>
References <CA+YdQ_6YMfM1_bsPntYgCafG-0R_ocMQpgX_Ez3p4Z1pg9LjVw@mail.gmail.com>
Date Tue, 15 Jan 2013 14:31:25 +0000
Subject Re: i can't understand decorator
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
To contro opinion <contropinion@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.539.1358260288.2939.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358260288 news.xs4all.nl 6975 [2001:888:2000:d::a6]:58251
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:36855

Show key headers only | View raw


On 15 January 2013 14:20, contro opinion <contropinion@gmail.com> wrote:
>>>> def deco(func):
> ...      def kdeco():
> ...          print("before myfunc() called.")
> ...          func()
> ...          print("  after myfunc() called.")
> ...      return kdeco
> ...
>>>> @deco
> ... def myfunc():
> ...      print(" myfunc() called.")
> ...
>>>> myfunc()
> before myfunc() called.
>  myfunc() called.
>   after myfunc() called.
>>>> deco(myfunc)()
> before myfunc() called.
> before myfunc() called.
>  myfunc() called.
>   after myfunc() called.
>   after myfunc() called.
> 1.
> why there are two lines :before myfunc() called.and tow lines :after
> myfunc() called. in the output?

You have wrapped the function twice with the decorator. Try changing the line
    print("before func() called")
to
    print("about to call", func,__name__)
and you'll see that the function it is about to call is not the same
in both cases.

> 2.why the result is not
> before myfunc() called.
>  myfunc() called.
>   after myfunc() called.
> before myfunc() called.
>  myfunc() called.
>   after myfunc() called.

You would get this output if you just called myfunc() twice. I don't
know why you expect wrapping the function twice to have this effect.


Oscar

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


Thread

Re: i can't understand decorator Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-15 14:31 +0000

csiph-web