Traversing Inheritance Model

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • digitalorganics@gmail.com

    #1

    Traversing Inheritance Model

    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?).Thank s all.

  • Ziga Seilnacht

    #2
    Re: Traversing Inheritance Model

    digitalorganics @gmail.com wrote:[color=blue]
    > 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?).Thank s all.[/color]

    The __mro__ descriptor does what you want. Example:
    [color=blue][color=green][color=darkred]
    >>> class A(object):[/color][/color][/color]
    .... pass
    ....[color=blue][color=green][color=darkred]
    >>> class B(object):[/color][/color][/color]
    .... pass
    ....[color=blue][color=green][color=darkred]
    >>> class C(A, B):[/color][/color][/color]
    .... pass
    ....[color=blue][color=green][color=darkred]
    >>> C.__mro__[/color][/color][/color]
    (<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>,
    <type 'object'>)

    See:

    and

    for details.

    Ziga

    Comment

    Working...