Question: Error or misconcept

Steve Holden sholden at
Mon Nov 5 16:25:29 CET 2001

"Károly Ladvánszky" <aa at> wrote in ...
> Thank you for your answer.
> > You've just created a class level shared object a1.
> Ok, but then I'd expect the same behaviour when a1 is a number.
> Try this:
> class c:
>     a1=0
>     def f1(self,a):
>         self.a1=a
> Now:
> o1=c()
> o1.f1(99)
> o1.a1 ==> 99
> o2=c()
> o2.a1 ==> 0 ! Shouldn't it reflect the change in the class level object's
> value just like in the case a1 is a list?
In your original two examples you were comparing the results of


in your first example with

    self.a1 = (a)

in your second example.

The fundamental difference is that the first example modifies the mutable
value bound to the name "self.a1", which is resolved as the class variable
a1. Your second example *rebinds" the name "self.a1", which is therefore
added to self's locals and becomes a separate variable from the class
variable. The class variable can in both instances be accessed using "c.a1".


More information about the Python-list mailing list