How to inheritance overwrite non virtual?

Axel Straschil axel at pizza.home.kosnet.com
Thu Sep 18 16:51:40 CEST 2003


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.




More information about the Python-list mailing list