X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109 Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:Python': 0.06; 'closest': 0.07; 'method,': 0.07; 'subject:two': 0.07; '>>>>': 0.09; 'decorator': 0.09; 'executed': 0.09; 'subject:both': 0.09; 'am,': 0.12; 'def': 0.15; 'subsequent': 0.15; '-tkc': 0.16; 'arg': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'important).': 0.16; 'message- id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'subject: \n ': 0.16; 'subject:function': 0.16; 'cc:addr:python- list': 0.16; 'wrote:': 0.16; 'cc:no real name:2**0': 0.20; 'header :In-Reply-To:1': 0.22; 'once.': 0.23; 'pm,': 0.24; 'aug': 0.24; 'stack': 0.24; 'function': 0.27; '(and': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'example': 0.30; 'cc:addr:gmail.com': 0.30; '"in': 0.30; 'decorators': 0.30; 'chris': 0.32; 'list': 0.32; 'it.': 0.33; 'actually': 0.33; 'calling': 0.33; 'header :User-Agent:1': 0.34; 'clarify': 0.34; 'function.': 0.34; 'subject:How': 0.35; 'fri,': 0.36; 'cc:2**1': 0.36; 'executing': 0.37; 'but': 0.37; 'question,': 0.38; 'subject:: ': 0.39; 'called': 0.40; "i'd": 0.40; "it's": 0.40; 'order': 0.62; 'below,': 0.64; 'here': 0.65; 'chain': 0.66; 'subject:call': 0.91; 'subject:want': 0.93 Date: Sat, 06 Aug 2011 07:19:27 -0500 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 MIME-Version: 1.0 To: Chris Rebert Subject: Re: How do I implement two decorators in Python both of which would eventually want to call the calling function References: <8b087624-4e05-4d2e-bf4f-78383202b7e1@28g2000pry.googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: python-list@python.org, Devraj X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312633195 news.xs4all.nl 23960 [2001:888:2000:d::a6]:58820 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10973 On 08/06/2011 02:49 AM, Chris Rebert wrote: > On Fri, Aug 5, 2011 at 10:49 PM, Devraj wrote: >> My question, how do I chain decorators that end up executing the >> calling method, but ensure that it's only called once. > > That's how it works normally; decorators stack (and order is therefore > important). With normal wrapping decorators, only the first decorator > gets access to the original function and is able to call it. I'd clarify "first decorator" here as the one closest to the decorated function which is actually the *last* one in the list of decorators, but first-to-decorate. In Chris's example below, decorator_A is the only one that calls myfunc(). > Subsequent decorators only get access to the already-wrapped function. > > Example: > > def decorator_A(func): > def decorated(*args, **kwds): > print "In decorator A" > return func(*args, **kwds) > return decorated > > def decorator_B(func): > def decorated(*args, **kwds): > print "In decorator B" > return func(*args, **kwds) > return decorated > > @decorator_B > @decorator_A > def myfunc(arg): > print "hello", arg > >>>> myfunc('bob') > In decorator B > In decorator A > hello bob > > > Notice that myfunc() only got executed once. -tkc