Visualize class inheritance hierarchy
Rob Kirkpatrick
robert.d.kirkpatrick at gmail.com
Wed Sep 24 02:05:29 EDT 2008
On Sep 23, 4:58 pm, Christian Heimes <li... at cheimes.de> 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)
> (<class 'sqlalchemy.types.DateTime'>, <class
> 'sqlalchemy.types.TypeEngine'>, <class 'sqlalchemy.types.AbstractType'>,
> <type 'object'>
> >>> DateTime.__bases__
> (<class 'sqlalchemy.types.TypeEngine'>,)
>
> >>> pprint.pprint(inspect.getclasstree(inspect.getmro(DateTime)))
> [(<type 'object'>, ()),
> [(<class 'sqlalchemy.types.AbstractType'>, (<type 'object'>,)),
> [(<class 'sqlalchemy.types.TypeEngine'>,
> (<class 'sqlalchemy.types.AbstractType'>,)),
> [(<class 'sqlalchemy.types.DateTime'>,
> (<class 'sqlalchemy.types.TypeEngine'>,))]]]]
>
> >>> [cls for cls in inspect.getmro(DateTime) if hasattr(cls, '__init__')]
> [<class 'sqlalchemy.types.DateTime'>, <class
> 'sqlalchemy.types.TypeEngine'>, <class 'sqlalchemy.types.AbstractType'>,
> <type 'object'>]
>
> >>> [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!
More information about the Python-list
mailing list