Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
doctests
  • Loading branch information
JelleZijlstra committed Jul 21, 2025
commit ca07bc9f84297b09430ab35cf1ad9db405496d96
88 changes: 53 additions & 35 deletions Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1501,10 +1501,13 @@ Multiple inheritance
Python classes may have multiple base classes, a technique known as
*multiple inheritance*. The base classes are specified in the class definition
by listing them in parentheses after the class name, separated by commas.
For example, the following class definition::
For example, the following class definition:

class C(A, B):
pass
.. doctest::

>>> class A: pass
>>> class B: pass
>>> class C(A, B): pass

defines a class ``C`` that inherits from classes ``A`` and ``B``.

Expand All @@ -1518,18 +1521,28 @@ cannot be created, if no valid metaclass can be determined, or if there is an in
layout conflict. We'll discuss each of these in turn.

First, all base classes must allow subclassing. While most classes allow subclassing,
some built-in classes do not, such as :class:`bool`::
some built-in classes do not, such as :class:`bool`:

class SubBool(bool): # TypeError
pass
.. doctest::

>>> class SubBool(bool): # TypeError
... pass
Traceback (most recent call last):
...
TypeError: type 'bool' is not an acceptable base type

To create a consistent MRO, all bases appear in the order
they were specified in the base class list and every child class must appear before its
base classes. Below is an example where this fails::
base classes. Below is an example where this fails:

.. doctest::

class Base: pass
class Child(Base): pass
class Grandchild(Base, Child): pass # TypeError
>>> class Base: pass
>>> class Child(Base): pass
>>> class Grandchild(Base, Child): pass # TypeError
Traceback (most recent call last):
...
TypeError: Cannot create a consistent method resolution order (MRO) for bases Base, Child

In the MRO of ``Grandchild``, ``Child`` must appear before ``Base`` because it is first
in the base class list, but it must also appear after ``Base`` because it is a child of
Expand Down Expand Up @@ -1558,31 +1571,36 @@ for each base class to produce a list of candidate solid bases. If there is a un
that is a subclass of all others, then that class is the solid base. Otherwise, class creation
fails.

Example::

class Solid1:
__slots__ = ("solid1",)

class Solid2:
__slots__ = ("solid2",)

class SolidChild(Solid1):
__slots__ = ("solid_child",)

class C1: # solid base is `object`
pass

# OK: solid bases are `Solid1` and `object`, and `Solid1` is a subclass of `object`.
class C2(Solid1, C1): # solid base is `Solid1`
pass

# OK: solid bases are `SolidChild` and `Solid1`, and `SolidChild` is a subclass of `Solid1`.
class C3(SolidChild, Solid1): # solid base is `SolidChild`
pass

# Error: solid bases are `Solid1` and `Solid2`, but they are not subclasses of each other.
class C4(Solid1, Solid2): # error: no single solid base
pass
Example:

.. doctest::

>>> class Solid1:
... __slots__ = ("solid1",)
>>>
>>> class Solid2:
... __slots__ = ("solid2",)
>>>
>>> class SolidChild(Solid1):
... __slots__ = ("solid_child",)
>>>
>>> class C1: # solid base is `object`
... pass
>>>
>>> # OK: solid bases are `Solid1` and `object`, and `Solid1` is a subclass of `object`.
>>> class C2(Solid1, C1): # solid base is `Solid1`
... pass
>>>
>>> # OK: solid bases are `SolidChild` and `Solid1`, and `SolidChild` is a subclass of `Solid1`.
>>> class C3(SolidChild, Solid1): # solid base is `SolidChild`
... pass
>>>
>>> # Error: solid bases are `Solid1` and `Solid2`, but they are not subclasses of each other.
>>> class C4(Solid1, Solid2): # error: no single solid base
Comment thread
AlexWaygood marked this conversation as resolved.
Outdated
... pass
Traceback (most recent call last):
...
TypeError: multiple bases have instance lay-out conflict

.. _async:

Expand Down