473,390 Members | 1,176 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,390 software developers and data experts.

How to determine which method was used in an inheritance heirarchy?

Say you're given a call event frame for a method call. How can you
tell if the code being executed came from a super class of the object
or class the method was called on?

Erik Jones

Software Developer | Emmaź
er**@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
Jul 16 '07 #1
4 1402
On Jul 16, 5:51 am, Erik Jones <e...@myemma.comwrote:
Say you're given a call event frame for a method call. How can you
tell if the code being executed came from a super class of the object
or class the method was called on?

Erik Jones
You look if the method was defined in self.__class__.__dict__.

Michele Simionato

Jul 16 '07 #2

On Jul 15, 2007, at 11:23 PM, Michele Simionato wrote:
On Jul 16, 5:51 am, Erik Jones <e...@myemma.comwrote:
>Say you're given a call event frame for a method call. How can you
tell if the code being executed came from a super class of the object
or class the method was called on?

Erik Jones

You look if the method was defined in self.__class__.__dict__.

Michele Simionato
That doesn't seem to cover calling super class __init__ methods.

Erik Jones

Software Developer | Emmaź
er**@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
Jul 16 '07 #3
On Jul 16, 7:18 am, Erik Jones <e...@myemma.comwrote:
On Jul 15, 2007, at 11:23 PM, Michele Simionato wrote:
On Jul 16, 5:51 am, Erik Jones <e...@myemma.comwrote:
Say you're given a call event frame for a method call. How can you
tell if the code being executed came from a super class of the object
or class the method was called on?
Erik Jones
You look if the method was defined in self.__class__.__dict__.
Michele Simionato

That doesn't seem to cover calling super class __init__ methods.
I am probably missing something. In the following code the
method check_init checks if the current instance
possess an __init__ or if it just inherits one
from the ancestors. Is this what you want?

class B(object):
def __init__(self):
'something'
def check_init(self):
if '__init__' in self.__class__.__dict__:
print 'possesses __init__'
else:
print 'inherits __init__'

class C(B):
'something else'
def __init__(self):
print 'calling C.__init__'

class D(B):
pass

c = C()
d = D()

c.check_init() #possesses __init__
d.check_init() #inherits __init__

Jul 16 '07 #4
On Jul 16, 2007, at 12:53 AM, Michele Simionato wrote:
On Jul 16, 7:18 am, Erik Jones <e...@myemma.comwrote:
>On Jul 15, 2007, at 11:23 PM, Michele Simionato wrote:
>>On Jul 16, 5:51 am, Erik Jones <e...@myemma.comwrote:
Say you're given a call event frame for a method call. How can you
tell if the code being executed came from a super class of the
object
or class the method was called on?
>>>Erik Jones
>>You look if the method was defined in self.__class__.__dict__.
>> Michele Simionato

That doesn't seem to cover calling super class __init__ methods.

I am probably missing something. In the following code the
method check_init checks if the current instance
possess an __init__ or if it just inherits one
from the ancestors. Is this what you want?

class B(object):
def __init__(self):
'something'
def check_init(self):
if '__init__' in self.__class__.__dict__:
print 'possesses __init__'
else:
print 'inherits __init__'

class C(B):
'something else'
def __init__(self):
print 'calling C.__init__'

class D(B):
pass

c = C()
d = D()

c.check_init() #possesses __init__
d.check_init() #inherits __init__
Ok, I see how I was pretty vague with my original questions. Given
the pattern where you need to call a base class's constructor (or,
other overriden method of the same name as that being called on the
child class object):

class A(object):
def __init__(self):
print self.__class__.__name__

class B(A):
def __init__(self):
A.__init__(self)
print self.__class__.__name__

B()

This will output:

B
B

How can I get

A
B

Erik Jones

Software Developer | Emmaź
er**@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
Jul 16 '07 #5

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

Similar topics

9
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__...
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
4
by: bonono | last post by:
Hi, Suppose my class definition is like this : class A: name = "A" @classmethod def foo(cls): cls.__super.foo()
6
by: Donal McWeeney | last post by:
I was under the impression that in an interface declaration I could inherit another interface declaration and the result would be that the inheriting interface could include the methods and...
0
by: Faraz | last post by:
Is it possible to create a inheritance heirarchy for the web service proxy class that inherits the SoapHttpClientProtocol?? I am trying to create a superclass that inherits from...
4
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
5
by: David Palau | last post by:
I'm looking for some guidance on what data type would work best as the result output of a web service function method. This web method will return an object that implements a class that is...
4
by: interec | last post by:
I have trying to set up a three level inheritance heirarchy (see files test.h/cpp below). When I compile this code using g++, I get the following errors: bash-3.00$ g++ test.cpp test.cpp: In...
9
by: VK | last post by:
<OT>I am finishing TransModal 0.1 so planning to move it from alpha to beta stage.<OT> Besides that I am planning to write an introductory to inheritance schema currently used in Javascript...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
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...

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.