MRO Error on Multiple Inheritance?

M

Ming

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....
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.
 
F

Fredrik Lundh

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:

</F>
 
B

Ben Finney

Ming said:
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...
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
 
M

Michele Simionato

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
...
...
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/
 
M

Ming

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

Ming said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top