How to inheritance overwrite non virtual?

Peter Otten __peter__ at
Thu Sep 18 17:53:35 CEST 2003

Axel Straschil wrote:

> Hi!
>> The following will do:
>> class Derived(Base):
>>     def method(self):
>>         return Base.method(self) # call base class method
> I tried in my chield-class:
> class K2(KObject):
> def bestCaption(self):
> return "Overwritten"
> def bestCaptionlong(self):
> return KObject.bestCaptionlong(self)
> The Problem ist inside of KObject.bestCaptionlong(self):
>   return self.bestCaption()
> The mother-class is calling the method of the child-class, not
> it own method. I want the mother-method to be called like "the
> chield never was existing", but I find no way.
> Anny other idea?
> Thank, AXEL.

What you want seems to be

class KObject:
    def bestCaptionlong(self):
        # class made explicit to guard against overriding
        return KObject.bestCaption(self) 

Some additional remarks: 
Do-nothing accessor methods may be acceptable style in C++ but are
superfluous in Python.
The abundance of xxxCaption and label attributes somewhat obscures your
design goal for me, but usually the ability to override a method to change
its effect is a feature, not something to fight against.



More information about the Python-list mailing list