Traversing Inheritance Model
Ziga Seilnacht
ziga.seilnacht at gmail.com
Tue Jun 27 01:39:23 EDT 2006
digitalorganics at gmail.com wrote:
> What's the best way to traverse the web of inheritance? I want to take
> a class and traverse its bases and then the bases' bases etc....
> looking for a particular class. What first came to mind was nested for
> loops. However, I want to know if there's some pre-existing method for
> doing this or if this isn't even possible (might send me in circles
> perhaps?).Thanks all.
The __mro__ descriptor does what you want. Example:
>>> class A(object):
... pass
...
>>> class B(object):
... pass
...
>>> class C(A, B):
... pass
...
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,
<type 'object'>)
See:
http://www.python.org/download/releases/2.2.3/descrintro/
and
http://www.python.org/download/releases/2.3/mro/
for details.
Ziga
More information about the Python-list
mailing list