In a python course they discuss classes and MRO. The following example fails
class A:
def info(self):
print('Class A')
class B(A):
def info(self):
print('Class B')
class C(A):
def info(self):
print('Class C')
class D(A, C):
pass
D().info()
with the error
Traceback (most recent call last):
File "main.py", line 13, in <module>
class D(A, C):
TypeError: Cannot create a consistent method resolution
order (MRO) for bases A, C
But the MRO is described to follow this path:
- Check for
info
inD
(since we callinfo
onD
) - if not, check for
info
in the super-classes, left to right
Why doesn't it just use the info
from A
? Switching A
to B
works fine
class D(B,C):
pass
D.info() # "Class B"