How to inheritance overwrite non virtual?

A

Axel Straschil

Hi!

I've got an class with a couple of depending methods.
I want to inheritance and create a new class where just
one method ist overwritten.

My problem: When python is executing the mother-method,
not the depending mother-method is taken, it take the
method of the chield.

In my example, i want that K2.bestCaptionlong() returns
the same as KObject.bestCaptionlong() - no way!

I've tryed (in K2) something like:
def bestCaptionlong(self):
return KObject().bestCaptionlong()
which I expected to be like KObject::bestCaptionlong() in
C++, but it doesn't work ;-(

Any Idea how to make the mother-class calling her method's?

Here is my Example:

class KObject:
def __init__(self):
self._label=None
self._caption=None
self._captionlong=None
def __str__(self):
return (
"label: [%s], %s, (%s)\n"+
"caption: [%s], %s, (%s)\n"+
"captionlong: [%s], %s, (%s)"
)%( self._label, self.label(), self.bestLabel(),
self._caption, self.caption(), self.bestCaption(),
self._captionlong, self.captionlong(), self.bestCaptionlong()
)
def label(self):
return self._label
def caption(self):
return self._caption
def captionlong(self):
return self._captionlong
def bestLabel(self):
return self.label()
def bestCaption(self):
if self.caption() is not None:
return self.caption()
else:
return self.bestLabel()
def bestCaptionlong(self):
if self.captionlong() is not None:
return self.captionlong()
else:
return self.bestCaption()

class K2(KObject):
def bestCaption(self):
return "Overwritten"

k = KObject()
k._label='x'

k2 = K2()
k2._label='x'

print k
print k2

Thanks, AXEL.
 
P

Peter Otten

Axel said:
I've tryed (in K2) something like:
def bestCaptionlong(self):
return KObject().bestCaptionlong()
which I expected to be like KObject::bestCaptionlong() in
C++, but it doesn't work ;-(

The following will do:

class Derived(Base):
def method(self):
return Base.method(self) # call base class method

(Of course you need not override the method if the body of the derived class
only calls the base class method.)

Peter
 
A

Axel Straschil

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.
 
P

Peter Otten

Axel said:
Hi!


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.

Peter




Peter
 
A

anton muhin

Axel said:
Hi!




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.

Change
return self.bestCaption()

to
return KObject.bestCaption(self)

hth,
anton.
 
R

Richie Hindle

[Axel]
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.

class A:
def bwahHaHa(self):
"""When called by callMe(), derived classes don't get a look in."""
print "Bwah-Ha-Ha!"

def callMe(self):
"""Calls non-overridable method bwahHaHa()."""
A.bwahHaHa(self) ########## This is the interesting bit ##########

class B(A):
def bwahHaHa(self):
"""Vain attempt to override what callMe() does."""
print "Squeak"

b = B()
b.callMe() # Prints "Bwah-Ha-Ha!"
b.bwahHaHa() # Prints "Squeak"
 
A

Axel Straschil

Hello!
What you want seems to be
class KObject:
def bestCaptionlong(self):
# class made explicit to guard against overriding
return KObject.bestCaption(self)

Yes, that behaves the way I excpected, thank's to you and the other
for helping me!
Do-nothing accessor methods may be acceptable style in C++ but are
superfluous in Python.

I'm afraid I'm still not thinking in python, I'll work on it ;-)

Lg, AXEL.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top