Skip to content

gh-136843: Document how multiple inheritance works #136844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 28, 2025
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
... pass
Traceback (most recent call last):
...
TypeError: multiple bases have instance lay-out conflict

.. _async:

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy