Unclear On Class Variables
Peter Hansen
peter at engcorp.com
Thu Jan 13 08:56:10 EST 2005
Simon Brunning wrote:
> On 13 Jan 2005 07:18:26 EST, Tim Daneliuk <tundra at tundraware.com> wrote:
> But you are being mislead by the fact that integers are immutable.
> 'spam.eggs = 2' is *creating* an instance member - there wasn't one
> before. Have a look at what happens with a mutable object:
Simon, it's really not about mutability at all. You've changed
the example, which was binding a name (specifically setting an
attribute), to one in which you are simply calling a method on
the object. If you change your example to bind the name the
same way, even with a mutable, it will work the same way as Tim's
original did with integers:
>>> class Spam(object):
... eggs = [3]
...
>>> spam = Spam()
>>> spam2 = Spam()
>>> spam.eggs = [7]
>>> spam2.eggs
[3]
-Peter
More information about the Python-list
mailing list