473,573 Members | 2,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I call a subclass method from parent class ?

Hi,

Suppose my class definition is like this :

class A:
name = "A"

@classmethod
def foo(cls):
cls.__super.foo ()
cls.bar()

@classmethod
def bar(cls):
print cls.name

class B(A):
name = "B"

class C(B):
name = "C"

What I want is

C.foo() prints 'ABC'
B.foo() prints 'BC'
A.foo() prints 'A'

But when I call C.foo(), it said

AttributeError: class C has no attribute '_A__super'

How should this be coded ?

Oct 20 '05 #1
4 11985
If you use a newstyle class, e.g. class A(object), then you can get the
superclass with cls.__base__. You could also use super(cls,cls),
although note that it returns a <super> object that isn't exactly the
same thing as a class -- but good enough for just accessing attributes.

Make sure to check that your superclass isn't <object>, otherwise it'll
complain about <object> not having a foo attribute. __base__ is
probably easier for this purpose. Also be careful with multiple
inheritance.

No such thing as __super though.

Oct 20 '05 #2
thanks, it works. Though I don't quite understand what super(cls,cls)
returns, and it doesn't work if I do a super(cls,cls). foo(). But
cls.__base__.fo o() do the trick.

thankfully, I don't have multiple inheritance.

Jason Lai wrote:
If you use a newstyle class, e.g. class A(object), then you can get the
superclass with cls.__base__. You could also use super(cls,cls),
although note that it returns a <super> object that isn't exactly the
same thing as a class -- but good enough for just accessing attributes.

Make sure to check that your superclass isn't <object>, otherwise it'll
complain about <object> not having a foo attribute. __base__ is
probably easier for this purpose. Also be careful with multiple
inheritance.

No such thing as __super though.


Oct 20 '05 #3
bo****@gmail.co m wrote:
Jason Lai wrote:
If you use a newstyle class, e.g. class A(object), then you can get the
superclass with cls.__base__. You could also use super(cls,cls),
although note that it returns a <super> object that isn't exactly the
same thing as a class -- but good enough for just accessing attributes.

Make sure to check that your superclass isn't <object>, otherwise it'll
complain about <object> not having a foo attribute. __base__ is
probably easier for this purpose. Also be careful with multiple
inheritance .

No such thing as __super though.


thanks, it works. Though I don't quite understand what super(cls,cls)
returns, and it doesn't work if I do a super(cls,cls). foo(). But
cls.__base__.fo o() do the trick.

thankfully, I don't have multiple inheritance.

In point of fact super() is most useful when you *do* have multiple
inheritance. The classic example is when classes B and C have a common
supertype A, and then class D is a subclass of both B and C.

Under these circumstances (the so-called "diamond-shaped inheritance
graph") it can be problematic to ensure that each superclass's methods
are called exactly once when methods are being extended rather than
overridden: should a D call B's method, which then calls A's, and if so
how does a D ensure that C's method gets called without it also calling
A's method.

Calls to super() are used to effectively place a linearised oredering on
the superclasses to ansure that the diamond-shaped inheritance pattern
is correctly handled to give the correct method resolution order.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 20 '05 #4
Thanks for the explanation but some how my code fail and since I don't
need multiple inheritance for the moment, I would settle for the not so
clean version.

The documentation of super is not very clear to me too. As seen in my
code, I am using classmethod which may cause some problem.

Steve Holden wrote:
In point of fact super() is most useful when you *do* have multiple
inheritance. The classic example is when classes B and C have a common
supertype A, and then class D is a subclass of both B and C.

Under these circumstances (the so-called "diamond-shaped inheritance
graph") it can be problematic to ensure that each superclass's methods
are called exactly once when methods are being extended rather than
overridden: should a D call B's method, which then calls A's, and if so
how does a D ensure that C's method gets called without it also calling
A's method.

Calls to super() are used to effectively place a linearised oredering on
the superclasses to ansure that the diamond-shaped inheritance pattern
is correctly handled to give the correct method resolution order.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


Oct 20 '05 #5

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

Similar topics

4
2797
by: lawrence | last post by:
I'm very unhappy with the error message that I'm giving in this method: /** * 11-23-03 - getter * * We want to run the query against a MySql database and get back a
4
353
by: Angelos Karantzalis | last post by:
Hi guys. I've come across a problem when I tried to serialize a class into xml, only to discover that the parent class's XML Serialization properties weren't included in the output xml. Actually, the class I'm serializing is two steps down in the inheritance ladder. It's got a parent class which also has a parent class :( All those classes...
7
2268
by: Jay Wolfe | last post by:
Hello, I'm guessing I can't do this, but can I do work in a subclass before its parent class initialization? I've got 2 classes, class A & subclass B: class A { public:
3
11867
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a to-be-developed-function cmdOkay_Click() function as described below. 1. Create an user control that contains an OK button as below Public Class...
4
6488
by: ad | last post by:
base only can call the parent 's method The result of the example below is I am Parent I am Child How can we call the grandparent's method in a inhreited class, I want the result is I am GrandParent
2
2306
by: jw56578 | last post by:
Is it possible to call a method from a parent object, but have the childs overriden method called instead of the base method? I want several children objects to all call a certain method when they are initialized, but they all have different implementations of that method. thanks
2
1839
by: Jack | last post by:
Hello, I have a 3-level class hirarchy: 1=Grandparent 2=Parent 3=Child The Grandparent has a method called OnPaint which I want to override from the Child class. I can do this, but how can I call the
5
3629
by: pixiedustjt | last post by:
Hello, This is my first time to post on the forum, so I hope I'm doing this correctly. I am working on the dreading Java Inventory Program that seems to be legendary. I am currently on part 3 and have most of the program working. I created an additional feature (book author) in a subclass, which is supposed to be called in my main class and...
6
14865
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
0
7705
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...
0
8205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8074
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...
0
6424
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5601
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5294
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...
0
3739
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2223
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1044
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...

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.