Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10960
| Date | 2011-08-06 09:49 +0200 |
|---|---|
| From | Rafael Durán Castañeda <rafadurancastaneda@gmail.com> |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1964.1312616975.1164.python-list@python.org> (permalink) |
You don't need doing something special, each decorator returns a new
function that and only the result function formed by all decorators will
be run. Consider this:
def clear_cache(func):
def decorator(*args, **kwargs):
print "cache cleared"
return func(*args, **kwargs)
return decorator
def login(func):
def decorator(*args, **kwargs):
print "login"
return func(*args, **kwargs)
return decorator
@login
@clear_cache
def post(msg= "POST", *args):
print msg
return args
result = post("POST", "something")
print result
The output will be:
login
cache cleared
POST
('something',)
On 06/08/11 07:49, 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.
>
> My question, how do I chain decorators that end up executing the
> calling method, but ensure that it's only called once.
>
> Appreciate any pointers and thanks for your time.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How do I implement two decorators in Python both of which would eventually want to call the calling function Devraj <devraj@gmail.com> - 2011-08-05 22:49 -0700 Re: How do I implement two decorators in Python both of which would eventually want to call the calling function Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-08-06 09:49 +0200 Re: How do I implement two decorators in Python both of which would eventually want to call the calling function Chris Rebert <clp2@rebertia.com> - 2011-08-06 00:49 -0700 Re: How do I implement two decorators in Python both of which would eventually want to call the calling function Peter Otten <__peter__@web.de> - 2011-08-06 09:59 +0200 Re: How do I implement two decorators in Python both of which would eventually want to call the calling function Tim Chase <python.list@tim.thechases.com> - 2011-08-06 07:19 -0500
csiph-web