How to inheritance overwrite non virtual?

Axel Straschil axel at
Thu Sep 18 16:51:40 CEST 2003


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):
	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()
			return self.bestLabel()
	def bestCaptionlong(self):
		if self.captionlong() is not None:
			return self.captionlong()
			return self.bestCaption()

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

k = KObject()

k2 = K2()

print k
print k2

Thanks, AXEL.

More information about the Python-list mailing list