Question: Error or misconcept
sholden at holdenweb.com
Mon Nov 5 16:25:29 CET 2001
"Károly Ladvánszky" <aa at bb.cc> 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:
> def f1(self,a):
> o1.a1 ==> 99
> 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