我在Django中有一个调用外部库/类的视图。问题是由于某些原因,Django保持缓存来自该类之前调用的结果。
请考虑以下简单示例:
Django观点:
from some_path import Demo
def test_view(request):
demo = Demo()
result = demo.do_something()
return render(request, 'test.html',
{ 'result':result }
)
演示课程:
class Demo():
result = []
def do_something(self):
self.result.append(1)
self.result.append(2)
self.result.append(3)
return self.result
你期望结果是[1,2,3],对吗? 错误!
第一次加载页面时,您将获得正确的结果。但是在以下所有请求中它将继续递增:[1,2,3,1,2,3] ...... [1,2,3,1,2,3,1,2,3] ......
所以我的问题很明显 - 这里发生了什么? 每当我在Django视图中调用一个类时,我如何收到[1,2,3]?
Django 1.7 / MacOS X。
答案 0 :(得分:11)
将result
中的__init__
定义为实例属性。
class Demo():
def __init__(self):
self.result = []
def do_something(self):
self.result.append(1)
self.result.append(2)
self.result.append(3)
return self.result
如果您在代码中打印result
,那么result
只会分配一次,
class Demo():
result = []
print result
def ...
.
.
d = Demo()
print d.do_something()
print d.do_something()
e = Demo()
print e.do_something()
>>>
[]
[1, 2, 3]
[1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
result
是一个可变对象,每当你创建一个类的实例时它将result
引用到同一个引用。
如果是Immutable对象: -
class Demo():
result = 1
def do_something(self):
self.result += 1
d = Demo()
d.do_something()
d.do_something()
e = Demo()
e.do_something()
print d.result, e.result
输出: -
>>>
3 2
答案 1 :(得分:1)
为什么不尝试在result=[]
方法中初始化do_something
这样的内容?
def do_something(self):
result = []
result.append(1)
result.append(2)
result.append(3)
return result
答案 2 :(得分:0)
"""
Case 1:
Here we have declared the result and some_string as static Class variable
"""
class Demo_1():
result = []
some_string = ""
def do_something(self):
Demo_1.some_string = Demo_1.some_string + "Python "
self.result.append(1)
self.result.append(2)
self.result.append(3)
return self.result
demo = Demo_1()
print Demo_1.result
result = demo.do_something()
print Demo_1.result
demo = Demo_1()
result = demo.do_something()
print Demo_1.result
demo = Demo_1()
result = demo.do_something()
print Demo_1.result
print result
print demo.some_string
print "Demo_1 class attributes :-", dir(Demo_1)
"""
Case1 Output :
[]
[1, 2, 3]
[1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
Python Python Python
Demo_1 class attributes :- ['__doc__', '__module__', 'do_something', 'result', 'some_string']
As you can see both result and some_string exists in Demo_1's attributes
which you can access by both by Class name as well as it's instance
"""
print "-----------------------------------------------------------------"
"""
Case 2:
Here we have declared the result variable as Class instance variable.
So each time we create an instance of class it creates new memory for that instance.
"""
class Demo_2():
def __init__(self):
self.result = []
self.some_string = " "
def do_something(self):
self.result.append(1)
self.result.append(2)
self.result.append(3)
return self.result
demo = Demo_2()
result = demo.do_something()
print demo.result
demo = Demo_2()
result = demo.do_something()
print demo.result
demo = Demo_2()
result = demo.do_something()
print demo.result
print result
print "Demo_1 class attributes :-", dir(Demo_2)
print Demo_2.some_string
"""
Case2 Output :
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
Demo_1 class attributes :- ['__doc__', '__init__', '__module__', 'do_something']
Traceback (most recent call last):
File "bl.py", line 83, in <module>
print Demo_2.some_string
AttributeError: class Demo_2 has no attribute 'some_string'
As you can see Class Demo_2 has no attributes result or some_string as
they are instance variable.
"""
详细了解静态类变量 Here