473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 11950
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__.foo() 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.com 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__.foo() 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
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
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. ...
7
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
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...
4
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...
2
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...
2
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...
5
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...
6
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...

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.