-1
class Seq:
    values = []

    def add(self, value):
        Seq.values.append(value)

I did the below in second cell of my Jupyter notebook

model = Seq()
model.add(1)
model.add(2)
model.add(3)

print(model.values)

I got this list printed out which is normal

[1, 2, 3]

But when I run the second cell again, the list keeps growing

[1, 2, 3, 1, 2, 3]

Run it again...

[1, 2, 3, 1, 2, 3, 1, 2, 3]

What am I not doing right?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 3
    `Seq.values` is a class attribute, it's shared by all instances. – Barmar Jul 03 '23 at 19:43
  • You need to add the line: `Seq.values = []` somewhere. (Or `Seq.values.clear()`) – quamrana Jul 03 '23 at 19:43
  • @Barmar but I created only one instance "model", the class attribute (list) keeps growing each time I call it on the instance which I don't want – Stephen Anyanwu Jul 03 '23 at 19:58
  • 1
    How are you "running the second cell again"? – Barmar Jul 03 '23 at 20:06
  • 1
    Does this answer your question? [How to avoid having class data shared among instances?](https://stackoverflow.com/questions/1680528/how-to-avoid-having-class-data-shared-among-instances) – slothrop Jul 03 '23 at 20:08
  • @StephenAnyanwu you created a second instance when you ran the cell a second time, because the first line of the cell is `model = Seq()` – slothrop Jul 03 '23 at 20:13

1 Answers1

2

Seq().values is persistent. No matter how many instances of Seq() you make, there is only one instance of values. This is because values is not tied to an instances of the class. It is tied to the class in general. I guess an analogy could be: if you had a Lung class with a class variable of air. You would be referring to all the air, the ONE air that we all breathe. No matter how many Lung instances you make they are all referring to the same ONE air. However if you had a self.air you would be referring only to the air in that lung.

You just need to make values related to the class instance, instead of the overall class. It also might be helpful to allow add to accept more than one value.

class Seq:
    def __init__(self):
        self.values = []

    def add(self, *values):
        self.values += values

CELL_EXECUTIONS = 100
for _ in range(CELL_EXECUTIONS):       
    model = Seq()
    model.add(1)
    model.add(2)
    model.add(3, 4, 5, 6)

print(model.values) #[1,2,3,4,5,6]
OneMadGypsy
  • 4,640
  • 3
  • 10
  • 26
  • I think this is actually related to what persists between cells in Jupyter. – Barmar Jul 03 '23 at 20:06
  • @Barmar - could you explain how what I did would affect that, because I do not use Jupyter. I want to give the proper answer. I don't need a dissertation - a butt-simple explanation will more than suffice. – OneMadGypsy Jul 03 '23 at 20:08
  • @Barmar I suspect Jupyter isn't any different in this respect from (say) pasting the "second cell" code into a REPL, executing it, then pasting again and executing again. That would give the same behaviour that OP shows in the question. For example: https://ideone.com/dF0Wn9 – slothrop Jul 03 '23 at 20:11
  • So, my answer is simply over-engineered but otherwise fine? – OneMadGypsy Jul 03 '23 at 20:11
  • I don't use Jupyter either, so I don't know what the issues are. From what I've seen, some things are supposed to restart afresh, the OP seems to think they're doing that. – Barmar Jul 03 '23 at 20:13
  • @Barmar - I will leave as-is for now, and adjust if the OP has issues. If something about my answer is wrong, I will have more data regarding exactly what that is when the OP complains. – OneMadGypsy Jul 03 '23 at 20:15
  • @Barmar broadly, things only restart afresh when you explicitly choose to restart the kernel. Otherwise, when you run a cell, it starts with whatever the globals were after the last cell execution, so there's no guarantee that the code in a cell is idempotent. – slothrop Jul 03 '23 at 20:15
  • @OneMadGypsy your answer appears to work fine, I'll go with it. Thanks – Stephen Anyanwu Jul 03 '23 at 20:24
  • @StephenAnyanwu - care to select it (big check mark) so I didn't spend my time on this for nothing? – OneMadGypsy Jul 03 '23 at 20:42