471,337 Members | 1,120 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 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 1344
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Lenard Lindstrom | last post: by
4 posts views Thread by KInd | last post: by
6 posts views Thread by Donal McWeeney | last post: by
4 posts views Thread by ddtl | last post: by
4 posts views Thread by interec | last post: by
9 posts views Thread by VK | last post: by
reply views Thread by rosydwin | last post: by

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.