R. David Murray added the comment:

Yep, this is the way it works.  When a class attribute name is referenced on an 
instance object, you are referencing the object pointed to by the class name.  
What happens next depends on what kind of object you have, and what kind of 
operation you perform.  In this case, you have an immutable object, and the 
addition operation returns a new immutable object, which gets assigned (as 
always happens) to the instance attribute name.  If tot was, say, [0] and the 
operation was self.tot += [1], your print would print [0, 1] [0, 1], because 
the in-place addition operation on lists *mutatates* the list, and what gets 
assigned to the instance attribute name is a pointer to the *same* object that 
the class attribute points to.

In short, this is a more subtle instance of the confusion discussed here: 
https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x
 (and also the FAQ that follows that one).

----------
nosy: +r.david.murray
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue23824>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to