This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Lists in class objects not independent
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: austin1howard, rhettinger
Priority: normal Keywords:

Created on 2011-06-01 01:39 by austin1howard, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
python_bug.py austin1howard, 2011-06-01 01:39 Example of bug report
Messages (2)
msg137410 - (view) Author: Austin Howard (austin1howard) Date: 2011-06-01 01:39
When creating a class which contains elements that are lists, the lists for different instances of the class are not independent. Calling
self.mylist.append(3)
inside a class method will update the mylist variable for *all* instances of the class, not just the current instance. Attached is a script which demonstrates this behavior.
msg137412 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-06-01 02:20
Sorry, that is correct behavior for class variables.

To get your intended effect (independent lists) do the list instantiation inside the __init__ method.
History
Date User Action Args
2022-04-11 14:57:18adminsetgithub: 56442
2011-06-01 02:20:58rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg137412

resolution: not a bug
2011-06-01 01:39:52austin1howardcreate