Question: Error or misconcept
Emile van Sebille
emile at fenx.com
Mon Nov 5 08:52:48 EST 2001
"Károly Ladvánszky" <aa at bb.cc> wrote in message
news:3be69395_1 at corp-goliath.newsgroups.com...
> 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
You will shortly create an instance variable a1. When this happens, you no
longer reach the class variable from instance references. Test further by
creating a2 at the class level.
> def f1(self,a):
> self.a1=a
>
> Now:
> o1=c()
> o1.f1(99)
you've just set the instance's a1 - not c.a1 (try it!)
> 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?
No. But after adding a2 (as per above), try:
o2.a2 # should be as init'd in class c
c.a2 = 2
o2.a2 # s/b 2
o1.a2 # s/b 2
The two places to find instance attributes are in the instance, the class,
and in __bases__ of class. No wait, that's three... ok... The three places
to find instance attributes are in the instance, the class, in __bases__ of
class, and in the module. No wait, that's four... ok... The four places to
find instance attributes are in the instance, the class, in __bases__ of
class, in the module, and in builtin. There! ;-)
Bases-search-order-subject-to-python-release-ly y'rs,
--
Emile van Sebille
emile at fenx.com
---------
More information about the Python-list
mailing list