Inheritance error in python 2.3.4???

friedmud at gmail.com friedmud at gmail.com
Mon Feb 14 18:14:12 EST 2005


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).  Then you inherit that
base class to make a sub class and the subclass contains a "set"
function on that same attribute.  If you call the set function and give
it a value and then call the get function......... it doesn't do what
you would expect.... instead of returning the value that was "set" it
instead returns the default value of the variable in the base class!!!

BUT!  If you implement the get function in the derived class it works
fine....

This, to me, is completely wrong.

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!".

Now if I re-implement getSomething in the subclass it does this:
####################################
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

        def getSomething( self ):
                return self.__Something


anObject = subClass()
anObject.setSomething("Cool!")
print anObject.getSomething()

bash-2.05b$ python main.py
Cool!
#######################################

Note that it now prints "Cool!" like it was supposed to in the first
place...

Hello?  Am I just being retarded?  To me the call to self.__Something =
aSomething should change THE ONLY instance of __Something to "Cool!".
Calling an inherited function shouldn't create a new instance of
__Something (which is what it has to be doing) and return that instead.

This is really going to cripple my object model if I have to
reimplement each one of these functions everytime!  The whole point was
to have a stable "implementation base" that captures a lot of the
common functionality between the different inherited classes... and
provides a "one stop shop" for modifying a lot of the behavior.  Also I
would have a lot less code because I would inherit functionality.

FYI - I am coming from a C++ background... where I do this kind of
thing often.

Someone please show me the pythonish way to do this!

Thanks!
Friedmud




More information about the Python-list mailing list