Re: Django strange behaviour

34 views
Skip to first unread message

monoBOT

unread,
Nov 8, 2014, 12:07:04 PM11/8/14
to django...@googlegroups.com

Lists are inmutable!!!! You are modifiying the same list everytime you invoque that view

El 07/11/2014 10:08, "termopro" <term...@gmail.com> escribió:

I have a view in Django which calls external library/class. The problem is that for some reason Django keeps caching results coming from previous calls of that class.

Please consider the following simple example:

Django view:

from some_path import Demo
def test_view(request):
    demo = Demo()
    result = demo.do_something()
    return render(request, 'test.html',
                            { 'result':result }
                )

Demo class:

class Demo():
    result = []

    def do_something(self):
        self.result.append(1)
        self.result.append(2)
        self.result.append(3)
        return self.result

You expect result to be [1, 2, 3], right ? WRONG!

The first time a page is loaded you'll get the correct result. But on all following requests it will keep incrementing: [1, 2, 3, 1, 2, 3]... [1, 2, 3, 1, 2, 3, 1, 2, 3] ...

So my question is obvious - what is going on here ? How do i receive [1, 2, 3] every time i call a class inside Django view ?

p.s. - Django 1.7 / MacOSx

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72ee993f-cc34-4aee-9455-694b09148d8d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aliane Abdelouahab

unread,
Nov 8, 2014, 12:20:52 PM11/8/14
to django...@googlegroups.com
you are calling the function twice, appending every element, and then appending the whole list!

Aliane Abdelouahab

unread,
Nov 8, 2014, 12:25:30 PM11/8/14
to django...@googlegroups.com
here is what is gives in the console:

class Demo():
    result = []

    def do_something(self):
        self.result.append(1)
        self.result.append(2)
        self.result.append(3)
        return self.result
    

demo = Demo()

result = demo.do_something()

result
[1, 2, 3]

demo = Demo()

result = demo.do_something()

demo = Demo()

result

Rafał Pitoń

unread,
Nov 8, 2014, 12:28:04 PM11/8/14
to django...@googlegroups.com
This happens because "result" is class level attribute, not instance one, and its expected behaviour in Python.

To made result instance attribute, define it in your __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

Scot Hacker

unread,
Nov 9, 2014, 12:40:37 PM11/9/14
to django...@googlegroups.com


On Saturday, November 8, 2014 9:07:04 AM UTC-8, monoBOT monoBOT wrote:

Lists are inmutable!!!!

Lists are absolutely mutable in Python - hence the availability of `result.append()`. 

Perhaps you're thinking of tuples, which are not mutable and have no methods for alteration. 

./s




Reply all
Reply to author
Forward
0 new messages