Inheritance error in python 2.3.4???
Fredrik Lundh
fredrik at pythonware.com
Mon Feb 14 18:25:54 EST 2005
friedmud at gmail.com wrote:
> In trying to construct a good object model in a recent project of mine,
> I ran across the following peculiarity in python 2.3.4 (haven't tried
> any newer versions):
>
> Say you have a base class that has an attribute and an accessor
> function for that attribute (just a simple get).
(don't use getters and setters methods in Python; use bare attributes where
you can, and properties when you need to add logic)
> BUT! If you implement the get function in the derived class it works
> fine....
>
> This, to me, is completely wrong.
it works exactly as documented.
> I have worked up the following example to illustrate my point:
>
> First is the way I want to do it:
> ######################################
> bash-2.05b$ cat main.py
> class baseClass(object):
> __Something = "Dumb!"
>
> def getSomething( self ):
> return self.__Something
>
> class subClass(baseClass):
> def setSomething( self , aSomething ):
> self.__Something = aSomething
>
> anObject = subClass()
> anObject.setSomething("Cool!")
> print anObject.getSomething()
>
> bash-2.05b$ python main.py
> Dumb!
> ###################################
>
> Note that it prints "Dumb!" instead of "Cool!".
members that start with __ (two underscores) are private to the class, so
you're in fact working with two different attributes here.
see section 9.6 in the tutorial for more on this:
http://docs.python.org/tut/node11.html#SECTION0011600000000000000000
to fix your problem, rename the attribute.
</F>
More information about the Python-list
mailing list