MRO Error on Multiple Inheritance?

Discussion in 'Python' started by Ming, Jan 4, 2008.

  1. Ming

    Ming Guest

    I'm working through Wesley Chun's CPP2e and got this error on 13.11.1,
    pp 548 where his interpreter snippet shows no problems:

    ActivePython 2.5.1.1 (ActiveState Software Inc.) b
    Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [
    win32
    Type "help", "copyright", "credits" or "license" f
    >>> class A(object): pass

    ....
    >>> class B(A): pass

    ....
    >>> class C(B): pass

    ....
    >>> class D(A, B): pass

    ....
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
    order (MRO) for bases A, B

    (I submitted the problem to the author but I'm not sure I'll ever hear
    back.) I'm guessing that this kind of diamond inheritance is
    prohibited by the interpreter, and that his lack of error messages
    from the interpretation is due to actually leaving out the "class
    B(A): pass" Can someone shed light? Thanks.
    Ming, Jan 4, 2008
    #1
    1. Advertising

  2. Ming wrote:


    > TypeError: Error when calling the metaclass bases
    > Cannot create a consistent method resolution
    > order (MRO) for bases A, B
    >
    > (I submitted the problem to the author but I'm not sure I'll ever hear
    > back.) I'm guessing that this kind of diamond inheritance is
    > prohibited by the interpreter, and that his lack of error messages
    > from the interpretation is due to actually leaving out the "class
    > B(A): pass"


    or, alternatively, leaving out the (object) in the first class definition:

    >>> class A: pass

    ....
    >>> class B(A): pass

    ....
    >>> class D(A, B): pass

    ....
    >>>


    </F>
    Fredrik Lundh, Jan 4, 2008
    #2
    1. Advertising

  3. Ming

    Ben Finney Guest

    Ming <> writes:

    > I'm working through Wesley Chun's CPP2e and got this error on 13.11.1,
    > pp 548 where his interpreter snippet shows no problems:


    I don't know what a "CPP2e" is. Is it a book? Can you give the ISBN?

    > ActivePython 2.5.1.1 (ActiveState Software Inc.) b
    > Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [
    > win32
    > Type "help", "copyright", "credits" or "license" f
    > >>> class A(object): pass

    > ...
    > >>> class B(A): pass

    > ...
    > >>> class C(B): pass

    > ...
    > >>> class D(A, B): pass

    > ...
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: Error when calling the metaclass bases
    > Cannot create a consistent method resolution
    > order (MRO) for bases A, B
    >
    > (I submitted the problem to the author but I'm not sure I'll ever hear
    > back.) I'm guessing that this kind of diamond inheritance is
    > prohibited by the interpreter, and that his lack of error messages
    > from the interpretation is due to actually leaving out the "class
    > B(A): pass" Can someone shed light? Thanks.


    That's not an example of diamond inheritance
    <URL:http://en.wikipedia.org/wiki/Diamond_problem> because classes A
    and B are not distinct classes with a *common* base. Instead, they're
    in a direct parent-child relationship.

    So there's no sense in defining class D to inherit from both A *and*
    B. To get a descendent of both those classes, inheriting from B is
    sufficient. It should rather be::

    class D(B): pass

    --
    \ "Pinky, are you pondering what I'm pondering?" "Uh, I think so, |
    `\ Brain, but we'll never get a monkey to use dental floss." -- |
    _o__) _Pinky and The Brain_ |
    Ben Finney
    Ben Finney, Jan 4, 2008
    #3
  4. On Jan 4, 9:03 pm, Ming <> wrote:
    > I'm working through Wesley Chun's CPP2e and got this error on 13.11.1,
    > pp 548 where his interpreter snippet shows no problems:
    >
    > ActivePython 2.5.1.1 (ActiveState Software Inc.) b
    > Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [
    > win32
    > Type "help", "copyright", "credits" or "license" f>>> class A(object): pass
    > ...
    > >>> class B(A): pass

    > ...
    > >>> class C(B): pass

    > ...
    > >>> class D(A, B): pass

    >
    > ...
    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > TypeError: Error when calling the metaclass bases
    > Cannot create a consistent method resolution
    > order (MRO) for bases A, B
    >
    > (I submitted the problem to the author but I'm not sure I'll ever hear
    > back.) I'm guessing that this kind of diamond inheritance is
    > prohibited by the interpreter, and that his lack of error messages
    > from the interpretation is due to actually leaving out the "class
    > B(A): pass" Can someone shed light? Thanks.


    Yes, the MRO for new-style classes changed in Python 2.3, see
    http://www.python.org/download/releases/2.3/mro/
    Michele Simionato, Jan 5, 2008
    #4
  5. Ming

    Ming Guest

    Thanks for the all the replies. CPP2e is the Second Edition of the
    book "Core Python Programming."

    On Jan 4, 6:13 pm, Ben Finney <>
    wrote:
    > Ming <> writes:
    > > I'm working through Wesley Chun's CPP2e and got this error on 13.11.1,
    > > pp 548 where his interpreter snippet shows no problems:

    >
    > I don't know what a "CPP2e" is. Is it a book? Can you give the ISBN?
    >
    >
    >
    > > ActivePython 2.5.1.1 (ActiveState Software Inc.) b
    > > Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) [
    > > win32
    > > Type "help", "copyright", "credits" or "license" f
    > > >>> class A(object): pass

    > > ...
    > > >>> class B(A): pass

    > > ...
    > > >>> class C(B): pass

    > > ...
    > > >>> class D(A, B): pass

    > > ...
    > > Traceback (most recent call last):
    > > File "<stdin>", line 1, in <module>
    > > TypeError: Error when calling the metaclass bases
    > > Cannot create a consistent method resolution
    > > order (MRO) for bases A, B

    >
    > > (I submitted the problem to the author but I'm not sure I'll ever hear
    > > back.) I'm guessing that this kind of diamond inheritance is
    > > prohibited by the interpreter, and that his lack of error messages
    > > from the interpretation is due to actually leaving out the "class
    > > B(A): pass" Can someone shed light? Thanks.

    >
    > That's not an example of diamond inheritance
    > <URL:http://en.wikipedia.org/wiki/Diamond_problem> because classes A
    > and B are not distinct classes with a *common* base. Instead, they're
    > in a direct parent-child relationship.
    >
    > So there's no sense in defining class D to inherit from both A *and*
    > B. To get a descendent of both those classes, inheriting from B is
    > sufficient. It should rather be::
    >
    > class D(B): pass
    >
    > --
    > \ "Pinky, are you pondering what I'm pondering?" "Uh, I think so, |
    > `\ Brain, but we'll never get a monkey to use dental floss." -- |
    > _o__) _Pinky and The Brain_ |
    > Ben Finney
    Ming, Jan 6, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Perks and Sarah Mount

    MRO problems with diamond inheritance?

    John Perks and Sarah Mount, May 1, 2005, in forum: Python
    Replies:
    13
    Views:
    589
    Michele Simionato
    May 3, 2005
  2. Uwe Mayer

    don't understand MRO

    Uwe Mayer, Jun 23, 2005, in forum: Python
    Replies:
    3
    Views:
    408
    Martin Franklin
    Jun 24, 2005
  3. =?iso-8859-1?Q?Fran=E7ois?= Pinard

    How to devise run-time pluggable classes? (MRO problem)

    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Aug 20, 2005, in forum: Python
    Replies:
    0
    Views:
    325
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Aug 20, 2005
  4. Replies:
    8
    Views:
    6,840
    Michele Simionato
    Jun 27, 2006
  5. Clarence

    MRO theory

    Clarence, Apr 11, 2007, in forum: Python
    Replies:
    4
    Views:
    545
    Clarence
    Apr 12, 2007
Loading...

Share This Page