Please don't fake interpreter sessions, was Re: Can't do a multiline assignment!

Peter Otten __peter__ at web.de
Thu Apr 17 19:43:06 CEST 2008


Michael Torrie wrote:

That's not how Python actually works:

> >>> a=myclass(3)
> >>> b=myclass(6)
> >>> a.classvar1=9
> >>> a.classvar1
> 9
> >>> b.classvar1
> 9

What actually happens is that the a.classvar1 = 9 assignment creates an
instance variable that shades the classvar:

>>> class A(object):
...     alpha = 1
...
>>> a = A()
>>> b = A()
>>> a.alpha = "something else"
>>> a.alpha
'something else'
>>> b.alpha
1

So everything works as expected (until you start modifying mutable
classvars).

Peter



More information about the Python-list mailing list