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

  • SEARCH
  • Home
  • 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 9217987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:45:30+00:00 2026-06-18T02:45:30+00:00

I have decorator for my views which creates new instance of SomeClass and calls

  • 0

I have decorator for my views which creates new instance of SomeClass and calls original view function with this param.

def decorator(orig_func):
    def inner_func(request, *args, **kwargs):
        api = SomeClass(request)
        return orig_func(request, api, *args, **kwargs)

The problem is that properties (variables) inside SomeClass seem to be persistent across many requests – for example when view_one is executed it sets some properties inside apu (SomeClass) class. In the second request to view_two those properties are still set with values from first request. How I can avoid this? I need simple solution.


here is SomeClass:

class SomeClass:
    some_variable = None

    def __init__(self,value):
        #self.some_variable = None

    def setVariable(self,value):
        self.some_variable = value

    def getVariable(self):
        return self.some_variable

My proble was that when request was executed and the commented line was commented, some_variable was not neccessery None – it had the value set during previous request. So I wrote this commented line to “clear” the variable.

Now I have a question – is this safe? Is there a possibility that one request will clear this variable during another request is executed and it will be overriden in this another request to None?

  • 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-06-18T02:45:31+00:00Added an answer on June 18, 2026 at 2:45 am

    The following is the correct class definition you want.

    class SomeClass:
        def __init__(self,value):
            self.some_variable = value
    
        def setVariable(self,value):
            self.some_variable = value
    
        def getVariable(self):
            return self.some_variable
    

    This will set the attribute on the instance rather than on the class object.

    >>> a = SomeClass(5)
    >>> a.some_variable # Just as expected
    5
    >>> b = SomeClass(10)
    >>> b.some_variable # This is its own variable
    10
    >>> b.some_variable = 20 # This won't change 'a'
    >>> a.some_variable # Hasn't changed
    5
    

    I might also want to note that getters and setters are often not want you want when writing python since you can do instance.some_variable = 5 without the need of a getter or setter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following decorator and view which works fine. Decorator def event_admin_only(func): Checks
Say I have a decorator which causes the function to print out any exceptions
I have been using this excellent decorator for memoization, which I found on the
I have a simple view function, and a decorator for it, and I want
I have a decorated function (simplified version): class Memoize: def __init__(self, function): self.function =
I have a Django view that receives POSTs which do not need to have
Consider this Django view which will get a list of items associated to the
I have a collapsible div implemented in js, the function of which is show/hide
I have a Django application which liberally uses django.contrib.auth.decorators.login_required() and also django.views.decorators.http.require_http_methods() . The
I have a Django view in which I call my_model.save() in a single object

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.