Michael Foord <[email protected]> added the comment:
Because strings are immutable. Your list access (self.list.append) mutates the
existing list in place.
Because strings are immutable you += is exactly equivalent to the following
code:
self.string = self.string + str(i)
The first lookup of self.string actually looks up the class attribute (because
on a fresh instance there is no instance attribute). The class attribute is an
empty string. You then create a new string by adding the empty string to str(i)
and assign an *instance* attribute to str(i).
Because you haven't mutated the class attribute (strings are immutable) the
next time round with a new instance the whole process repeats and the first
lookup of self.string still finds the class attribute which is still an empty
string.
With your example code try the following:
print Lister.string
print Lister()
print Lister.string
You will see that the class attribute is unchanged in between instantiations.
The += on immutable objects probably doesn't quite behave how you expect.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue7800>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com