Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:09:21+00:00 2026-05-24T10:09:21+00:00

I am trying to simply my Web application handlers, by using Python decorators. Essentially

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:09:21+00:00Added an answer on May 24, 2026 at 10:09 am

    Each successive decorator receives the previously wrapped function, so the function itself only gets called once at the end of the chain. Here’s a simple example:

    def dec1(f):
        def wrapped():
            print 'dec1'
            return f()
        return wrapped
    
    def dec2(f):
        def wrapped():
            print 'dec2'
            return f()
        return wrapped
    
    @dec2
    @dec1
    def spam(): print 'spam'
    
    >>> spam()
    dec2
    dec1
    spam
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using IIS and trying to deploy a web application that needs authentication mode=Windows.
I am using contenteditable div elements in my web application and I am trying
I am trying to learn how to write more dynamic web sites that use
I am trying to use Spring Security 3.0.5 in my web application. Basically, I
I'm using Spring 3, and trying to set up a simple web-app using annotations
I'm trying to make a simple C# web server that, at this stage, you
I'm simply trying to crop a JPEG image (no scaling) using PHP. Here is
I am simply trying to draw a rectangle inside of a panel using flex4.
I have a unique issue - I am designing a web application that creates
I'm trying to debug an ASP.NET web application in this environment: Windows Server Standard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.