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.bestCaptionl ong() returns
the same as KObject.bestCap tionlong() - no way!
I've tryed (in K2) something like:
def bestCaptionlong (self):
return KObject().bestC aptionlong()
which I expected to be like KObject::bestCa ptionlong() 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=Non e
self._caption=N one
self._captionlo ng=None
def __str__(self):
return (
"label: [%s], %s, (%s)\n"+
"caption: [%s], %s, (%s)\n"+
"captionlon g: [%s], %s, (%s)"
)%( self._label, self.label(), self.bestLabel( ),
self._caption, self.caption(), self.bestCaptio n(),
self._captionlo ng, self.captionlon g(), self.bestCaptio nlong()
)
def label(self):
return self._label
def caption(self):
return self._caption
def captionlong(sel f):
return self._captionlo ng
def bestLabel(self) :
return self.label()
def bestCaption(sel f):
if self.caption() is not None:
return self.caption()
else:
return self.bestLabel( )
def bestCaptionlong (self):
if self.captionlon g() is not None:
return self.captionlon g()
else:
return self.bestCaptio n()
class K2(KObject):
def bestCaption(sel f):
return "Overwritte n"
k = KObject()
k._label='x'
k2 = K2()
k2._label='x'
print k
print k2
Thanks, AXEL.
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.bestCaptionl ong() returns
the same as KObject.bestCap tionlong() - no way!
I've tryed (in K2) something like:
def bestCaptionlong (self):
return KObject().bestC aptionlong()
which I expected to be like KObject::bestCa ptionlong() 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=Non e
self._caption=N one
self._captionlo ng=None
def __str__(self):
return (
"label: [%s], %s, (%s)\n"+
"caption: [%s], %s, (%s)\n"+
"captionlon g: [%s], %s, (%s)"
)%( self._label, self.label(), self.bestLabel( ),
self._caption, self.caption(), self.bestCaptio n(),
self._captionlo ng, self.captionlon g(), self.bestCaptio nlong()
)
def label(self):
return self._label
def caption(self):
return self._caption
def captionlong(sel f):
return self._captionlo ng
def bestLabel(self) :
return self.label()
def bestCaption(sel f):
if self.caption() is not None:
return self.caption()
else:
return self.bestLabel( )
def bestCaptionlong (self):
if self.captionlon g() is not None:
return self.captionlon g()
else:
return self.bestCaptio n()
class K2(KObject):
def bestCaption(sel f):
return "Overwritte n"
k = KObject()
k._label='x'
k2 = K2()
k2._label='x'
print k
print k2
Thanks, AXEL.
Comment