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
>>
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
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()
you are calling the function twice, appending every element, and then
appending the whole list!
Le samedi 8 novembre 2014 18:07:04 UTC+1, monoBOT monoBOT a écrit :
>
> Lists are inmutable You are modifiying the same list everytime you
> invoque that view
> El 07/11/2014 10:08, "termopro" >
Lists are inmutable You are modifiying the same list everytime you
invoque that view
El 07/11/2014 10:08, "termopro" 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
> Den 07/11/2014 kl. 10.08 skrev termopro :
>
> 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.
This is expected Python behaviour - see
https://docs.python.org/3/tutoria
6 matches
Mail list logo