Visualize class inheritance hierarchy

2008-09-23 Thread 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Visualize class inheritance hierarchy

2008-09-23 Thread Rob Kirkpatrick
On Sep 23, 4:58 pm, Christian Heimes <[EMAIL PROTECTED]> wrote:
> Rob Kirkpatrick wrote:
> > 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:
>
>  >>> import inspect
>  >>> import pprint
>  >>> from sqlalchemy.types import DateTime
>
>  >>> inspect.getmro(DateTime)
> (,  'sqlalchemy.types.TypeEngine'>, ,
> 
>  >>> DateTime.__bases__
> (,)
>
>  >>> pprint.pprint(inspect.getclasstree(inspect.getmro(DateTime)))
> [(, ()),
>   [(, (,)),
>[(,
>  (,)),
> [(,
>   (,))
>
>  >>> [cls for cls in inspect.getmro(DateTime) if hasattr(cls, '__init__')]
> [,  'sqlalchemy.types.TypeEngine'>, ,
> ]
>
>  >>> [cls for cls in inspect.getmro(DateTime) if hasattr(cls, 'adapt')]
> [, ]

Oh, yeah.  That's the stuff!
--
http://mail.python.org/mailman/listinfo/python-list