How do I implement two decorators in Python both of which would eventually want to call the calling function

Chris Rebert clp2 at rebertia.com
Sat Aug 6 03:49:59 EDT 2011


On Fri, Aug 5, 2011 at 10:49 PM, Devraj <devraj at gmail.com> 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



More information about the Python-list mailing list