Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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; 'method,': 0.07; 'subject:two': 0.07; 'python': 0.08; 'decorator': 0.09; 'executed': 0.09; 'executes': 0.09; 'none):': 0.09; 'subject:both': 0.09; 'essentially': 0.10; 'def': 0.15; 'method.': 0.15; 'subsequent': 0.15; 'arg': 0.16; 'decorators.': 0.16; 'important).': 0.16; 'instance)': 0.16; 'subject: \n ': 0.16; 'subject:function': 0.16; 'twice.': 0.16; 'weird.': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'cheers,': 0.18; '>>>': 0.18; 'appropriate': 0.20; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'incorrect': 0.23; 'once.': 0.23; 'pm,': 0.24; 'aug': 0.24; 'cache': 0.24; 'stack': 0.24; 'code': 0.25; 'function': 0.27; 'all,': 0.28; 'message-id:@mail.gmail.com': 0.29; '(and': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'definition': 0.30; '"in': 0.30; 'decorators': 0.30; 'email name:': 0.30; 'error': 0.32; 'chris': 0.32; 'it.': 0.33; 'that,': 0.33; 'calling': 0.33; "i've": 0.34; 'function.': 0.34; 'subject:How': 0.35; 'unless': 0.36; 'fri,': 0.36; 'doing': 0.36; 'checks': 0.37; 'executing': 0.37; 'using': 0.37; 'but': 0.37; 'question,': 0.38; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'user': 0.39; 'called': 0.40; "it's": 0.40; 'order': 0.62; 'chain': 0.66; 'response.': 0.67; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84; 'received:209.85.218.46': 0.91; 'received:mail- yi0-f46.google.com': 0.91; 'subject:call': 0.91; 'subject:want': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=7PV3eiuL+0OxToxjgNQT0WMTfEQha+Qo1eyLjQNgilo=; b=KEvA02FfCo+GU86WbEnFff/hdVhGNKl1pnVRgI9Ug884vA+3FjOj3Xq5qNsjuwRAn2 IO2/nrZbPFniW9c6UanzlO/JwAc5Dht9gxfLz2XWbzPZikO/7+mdyKqOXiyl+ZUszf6r 2DY7hUYCvKksdTwrtMWKV13IT0WB0s6tldQXY= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <8b087624-4e05-4d2e-bf4f-78383202b7e1@28g2000pry.googlegroups.com> References: <8b087624-4e05-4d2e-bf4f-78383202b7e1@28g2000pry.googlegroups.com> Date: Sat, 6 Aug 2011 00:49:59 -0700 X-Google-Sender-Auth: tVEIULZDMfoN2nqne4rQzjqAFOc Subject: Re: How do I implement two decorators in Python both of which would eventually want to call the calling function From: Chris Rebert To: Devraj Content-Type: text/plain; charset=UTF-8 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312617002 news.xs4all.nl 23918 [2001:888:2000:d::a6]:55592 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10961 On Fri, Aug 5, 2011 at 10:49 PM, Devraj wrote: > Hi all, > > I am trying to simply my Web application handlers, by using Python > decorators. > > Essentially I want to use decorators to abstract code that checks for > authenticated sessions and the other that checks to see if the cache > provider (Memcache in this instance) has a suitable response. > > Consider this method definition with the decorators: > > @auth.login_required > @cache.clear > def post(self, facility_type_id = None): > > auth.login_required checks to see if the user is logged in, otherwise > returns an appropriate error message, or executes the original > function. > > cache.clear would check to to see if the cache has a particular key > and drop that, before it executes the calling method. > > Both auth.login_required and cache.clear would want to eventually > execute the calling method (post). > > >From what I've read both, doing what I am doing now would execute the > calling method (post) twice. That's incorrect unless the decorators you're using are weird. > 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. 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. Cheers, Chris -- http://rebertia.com