@@ -1501,10 +1501,13 @@ Multiple inheritance
15011501Python classes may have multiple base classes, a technique known as
15021502*multiple inheritance *. The base classes are specified in the class definition
15031503by listing them in parentheses after the class name, separated by commas.
1504- For example, the following class definition::
1504+ For example, the following class definition:
15051505
1506- class C(A, B):
1507- pass
1506+ .. doctest ::
1507+
1508+ >>> class A : pass
1509+ >>> class B : pass
1510+ >>> class C (A , B ): pass
15081511
15091512defines a class ``C `` that inherits from classes ``A `` and ``B ``.
15101513
@@ -1518,18 +1521,28 @@ cannot be created, if no valid metaclass can be determined, or if there is an in
15181521layout conflict. We'll discuss each of these in turn.
15191522
15201523First, all base classes must allow subclassing. While most classes allow subclassing,
1521- some built-in classes do not, such as :class: `bool `::
1524+ some built-in classes do not, such as :class: `bool `:
15221525
1523- class SubBool(bool): # TypeError
1524- pass
1526+ .. doctest ::
1527+
1528+ >>> class SubBool (bool ): # TypeError
1529+ ... pass
1530+ Traceback (most recent call last):
1531+ ...
1532+ TypeError: type 'bool' is not an acceptable base type
15251533
15261534To create a consistent MRO, all bases appear in the order
15271535they were specified in the base class list and every child class must appear before its
1528- base classes. Below is an example where this fails::
1536+ base classes. Below is an example where this fails:
1537+
1538+ .. doctest ::
15291539
1530- class Base: pass
1531- class Child(Base): pass
1532- class Grandchild(Base, Child): pass # TypeError
1540+ >>> class Base : pass
1541+ >>> class Child (Base ): pass
1542+ >>> class Grandchild (Base , Child ): pass # TypeError
1543+ Traceback (most recent call last):
1544+ ...
1545+ TypeError: Cannot create a consistent method resolution order (MRO) for bases Base, Child
15331546
15341547In the MRO of ``Grandchild ``, ``Child `` must appear before ``Base `` because it is first
15351548in the base class list, but it must also appear after ``Base `` because it is a child of
@@ -1558,31 +1571,36 @@ for each base class to produce a list of candidate solid bases. If there is a un
15581571that is a subclass of all others, then that class is the solid base. Otherwise, class creation
15591572fails.
15601573
1561- Example::
1562-
1563- class Solid1:
1564- __slots__ = ("solid1",)
1565-
1566- class Solid2:
1567- __slots__ = ("solid2",)
1568-
1569- class SolidChild(Solid1):
1570- __slots__ = ("solid_child",)
1571-
1572- class C1: # solid base is `object`
1573- pass
1574-
1575- # OK: solid bases are `Solid1` and `object`, and `Solid1` is a subclass of `object`.
1576- class C2(Solid1, C1): # solid base is `Solid1`
1577- pass
1578-
1579- # OK: solid bases are `SolidChild` and `Solid1`, and `SolidChild` is a subclass of `Solid1`.
1580- class C3(SolidChild, Solid1): # solid base is `SolidChild`
1581- pass
1582-
1583- # Error: solid bases are `Solid1` and `Solid2`, but they are not subclasses of each other.
1584- class C4(Solid1, Solid2): # error: no single solid base
1585- pass
1574+ Example:
1575+
1576+ .. doctest ::
1577+
1578+ >>> class Solid1 :
1579+ ... __slots__ = (" solid1" ,)
1580+ >>>
1581+ >>> class Solid2 :
1582+ ... __slots__ = (" solid2" ,)
1583+ >>>
1584+ >>> class SolidChild (Solid1 ):
1585+ ... __slots__ = (" solid_child" ,)
1586+ >>>
1587+ >>> class C1 : # solid base is `object`
1588+ ... pass
1589+ >>>
1590+ >>> # OK: solid bases are `Solid1` and `object`, and `Solid1` is a subclass of `object`.
1591+ >>> class C2 (Solid1 , C1 ): # solid base is `Solid1`
1592+ ... pass
1593+ >>>
1594+ >>> # OK: solid bases are `SolidChild` and `Solid1`, and `SolidChild` is a subclass of `Solid1`.
1595+ >>> class C3 (SolidChild , Solid1 ): # solid base is `SolidChild`
1596+ ... pass
1597+ >>>
1598+ >>> # Error: solid bases are `Solid1` and `Solid2`, but they are not subclasses of each other.
1599+ >>> class C4 (Solid1 , Solid2 ): # error: no single solid base
1600+ ... pass
1601+ Traceback (most recent call last):
1602+ ...
1603+ TypeError: multiple bases have instance lay-out conflict
15861604
15871605.. _async :
15881606
0 commit comments