Class Initialization of a list of empty lists not updating

137 views Asked by At

I have a list that is made up of 4 deques with a set length. I have made that list with the hope that it will update as the other ones. Here is a simple example:

class foo():
    def __init__(self):
        self.buffer_size = 300
        self.a = collections.deque([0]*self.buffer_size, self.buffer_size
        self.b = collections.deque([0]*self.buffer_size, self.buffer_size
        self.c = collections.deque([0]*self.buffer_size, self.buffer_size
        self.d = collections.deque([0]*self.buffer_size, self.buffer_size

        self.buffers = [list(self.a), list(self.b), list(self.c), list(self.d)]

    def do_something(self):
        ## here I append to these deques so that the values in them changes
        for i in range(20):
            self.a.append(i)
            self.b.append(i)
        

     def _buffers(self):
         ## buffers will still be all zeros
         print(self.buffers)
         
F = foo()
F.do_something()
F._buffers()

My question is why is it still just zeros? How come it doesn't update when the deques inside of it are changed? Thoughts on how to make that happen?

1

There are 1 answers

0
Amanda.py On

Turns out that the problem was that I was converting the deques into lists in the initialization of self.buffers. I suspect that the reason that happens is that when that list is created it points to a totally different place in memory and no longer is effected by changes.