Discuss Appending object to list overwrites previous in the Python Programming forum on Dev Shed. Appending object to list overwrites previous Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Time spent in forums: 3 Days 6 h 10 m 21 sec
Reputation Power: 34
But if each is a new one that is created at that time, why are the attributes shared between them?
"Because your code tells it to create a new user object with shared attributes", is the literal answer. What you describe in words is something like this:
But what you've written in Python is something like this:
Each *user* is a new one created when you call User(), but the name, parent and level are not. They are the original ones created when Python processes your source code and reads the class User: section.
Each new user object has a bit inside it which says "name? point back to the User class to get the name. parent? point back to the User class to get the parent".
So you get a list of four User items, all sharing one name text. When you update one name, all four show the same name, because they are all told to store "name" in the same place in memory.