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?
The following is the correct class definition you want.
This will set the attribute on the
instancerather than on theclassobject.I might also want to note that
gettersandsettersare often not want you want when writing python since you can doinstance.some_variable = 5without the need of a getter or setter.