Visualize class inheritance hierarchy

R

Rob Kirkpatrick

Hi All,

I just finished debugging some code where I needed to determine why
one subclass had a bound method and another did not. They had
different pedigree's but I didn't know immediately what the
differences were.

I ended up walking the hierarchy, going back one class at a time
through the code, for the two subclasses (hierarchy ~7 classes deep
each) to see whom they inherited from. Short of writing this down on
paper, is there any way to graphically display the pedigree of an
object/class? "Graphically" can be text output to the terminal, don't
need anything special...

I'm assuming this has been discussed before, but I'm lacking any
Google keywords that bring up the appropriate discussion.

Cheers,
Rob
 
A

Aaron \Castironpi\ Brady

Hi All,

I just finished debugging some code where I needed to determine why
one subclass had a bound method and another did not.  They had
different pedigree's but I didn't know immediately what the
differences were.

I ended up walking the hierarchy, going back one class at a time
through the code, for the two subclasses (hierarchy ~7 classes deep
each) to see whom they inherited from.  Short of writing this down on
paper, is there any way to graphically display the pedigree of an
object/class?  "Graphically" can be text output to the terminal, don't
need anything special...

I'm assuming this has been discussed before, but I'm lacking any
Google keywords that bring up the appropriate discussion.

Cheers,
Rob

Compare their __mro__ members if they are new-style. The depth-first
search for the member you're looking for is straightforward in Python,
even if maybe not immediately obvious.

If not, we can try a settrace and a search for when they are defined,
and build an __mro__ manually.
 
A

Aaron \Castironpi\ Brady

Hi All,

I just finished debugging some code where I needed to determine why
one subclass had a bound method and another did not.  They had
different pedigree's but I didn't know immediately what the
differences were.

I ended up walking the hierarchy, going back one class at a time
through the code, for the two subclasses (hierarchy ~7 classes deep
each) to see whom they inherited from.  Short of writing this down on
paper, is there any way to graphically display the pedigree of an
object/class?  "Graphically" can be text output to the terminal, don't
need anything special...

I'm assuming this has been discussed before, but I'm lacking any
Google keywords that bring up the appropriate discussion.

Cheers,
Rob

If you're using new-style classes, check out the __mro__ member, which
no doesn't show up in its dir(). Otherwise, we can try a settrace
(frame.f_code.co_name has the name of the class being defined in a
class statement) or accumulate some class statements with a custom
'parser' run in the module 'parser'.
 
R

Rob Kirkpatrick

Rob said:
I'm assuming this has been discussed before, but I'm lacking any
Google keywords that bring up the appropriate discussion.

You are looking for "mro" aka method resolution order. The inspect
module contains several helper functions to inspect a class hierarchy.

The following interactive session should give you an impression how to
use the functions:
(<class 'sqlalchemy.types.DateTime'>, <class
[(<type 'object'>, ()),
[(<class 'sqlalchemy.types.AbstractType'>, (<type 'object'>,)),
[(<class 'sqlalchemy.types.TypeEngine'>,
(<class 'sqlalchemy.types.AbstractType'>,)),
[(<class 'sqlalchemy.types.DateTime'>,
( said:
[cls for cls in inspect.getmro(DateTime) if hasattr(cls, '__init__')]
[<class 'sqlalchemy.types.DateTime'>, <class
'sqlalchemy.types.TypeEngine'>, <class 'sqlalchemy.types.AbstractType'>,
[cls for cls in inspect.getmro(DateTime) if hasattr(cls, 'adapt')]
[<class 'sqlalchemy.types.DateTime'>, <class 'sqlalchemy.types.TypeEngine'>]

Oh, yeah. That's the stuff!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top