-3

I was working on a project and noticed a weird behavior while using classes in python, so I decided to take a closer look at it. So I'm instantiating some object A and assigning it to a variable and I keep doing this using a for loop. Weirdly enough, some of the attributes seemed to remember their values from the previous instances of the object, which didn't make any sense to me. I ran some tests and this is what I found: [1]: https://i.stack.imgur.com/HqfIm.png

The default value of the attribute is an empty list, so I expected it to be an empty list every time the object is reinstantied. Apparently, although the second object has a different id from the first one (which is to be expected) the attribute has the exact same id as before and this explains how the new object "remembers" the old attribute. Could anyone explain why this is happening? Wouldn't we expect the new object to forget everything about the previous one and set this attribute to be an empty list?

cabutchei
  • 1
  • 1

0 Answers0