473,804 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to inheritance overwrite non virtual?

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.
Jul 18 '05 #1
6 2211
Axel Straschil wrote:
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 ;-(


The following will do:

class Derived(Base):
def method(self):
return Base.method(sel f) # 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
Jul 18 '05 #2
Hi!
The following will do:

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


I tried in my chield-class:
class K2(KObject):
def bestCaption(sel f):
return "Overwritte n"
def bestCaptionlong (self):
return KObject.bestCap tionlong(self)

The Problem ist inside of KObject.bestCap tionlong(self):
return self.bestCaptio n()

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.
Jul 18 '05 #3
Axel Straschil wrote:
Hi!

The following will do:

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

I tried in my chield-class:
class K2(KObject):
def bestCaption(sel f):
return "Overwritte n"
def bestCaptionlong (self):
return KObject.bestCap tionlong(self)

The Problem ist inside of KObject.bestCap tionlong(self):
return self.bestCaptio n()

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.bestCaptio n()

to
return KObject.bestCap tion(self)

hth,
anton.

Jul 18 '05 #4
Axel Straschil wrote:
Hi!
The following will do:

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


I tried in my chield-class:
class K2(KObject):
def bestCaption(sel f):
return "Overwritte n"
def bestCaptionlong (self):
return KObject.bestCap tionlong(self)

The Problem ist inside of KObject.bestCap tionlong(self):
return self.bestCaptio n()

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.bestCap tion(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
Jul 18 '05 #5

[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"

--
Richie Hindle
ri****@entrian. com
Jul 18 '05 #6
Hello!
What you want seems to be
class KObject:
def bestCaptionlong (self):
# class made explicit to guard against overriding
return KObject.bestCap tion(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.
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3221
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
4
2903
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
2
2696
by: Søren Holstebroe | last post by:
Hi there, I'm having a struggle with GNU g++ (3.3.5) and inheritance of typedefs in STL containers. I'm trying to port some old code I wrote with MS Visual C++ and it looks like there is a discrepancy in the STL implementation or at least interpretation of the standard (I trust g++ most on the latter). To the case: While this compiles fine:
22
23390
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
14
1931
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
3
4553
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape = base class Triangle, Square = classes derived from Shape Prism = class derived from Shape TriangularPrism, SquarePrism = classes derived from Triangle and Prism, or Square and Prism respectively
12
2666
by: mijobee | last post by:
I'm very new to c++ and just writing some code to learn. I've run into a problem, with a javaish design, and want to know if there is any possible solution without modifying the design. I've read up on virtual inheritance and from my understanding this should work fine but I haven't found any docs that use such a tangled example. The gcc output containing the errrors: Example.cpp: In member function 'virtual IObject*...
23
4618
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
3
2555
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
5
1894
by: Lars Hillebrand | last post by:
Hello, i discovered a weird behaviour if i use templates together with virtual inheritance and method over. I managed to reproduce my problem with a small example: // *********** <code example********** template<typename Tclass TypedInterface { public: virtual void TestFunction( T * ) = 0;
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10359
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10101
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6870
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.