@@ -28,81 +28,80 @@ An :func:`issubclass` or :func:`isinstance` test for an interface works in one
2828of three ways.
2929
30301) A newly written class can inherit directly from one of the
31- abstract base classes. The class must supply the required abstract
32- methods. The remaining mixin methods come from inheritance and can be
33- overridden if desired. Other methods may be added as needed:
31+ abstract base classes. The class must supply the required abstract
32+ methods. The remaining mixin methods come from inheritance and can be
33+ overridden if desired. Other methods may be added as needed:
3434
35- .. testcode ::
35+ .. testcode ::
3636
37- class C(Sequence): # Direct inheritance
38- def __init__(self): ... # Extra method not required by the ABC
39- def __getitem__(self, index): ... # Required abstract method
40- def __len__(self): ... # Required abstract method
41- def count(self, value): ... # Optionally override a mixin method
37+ class C(Sequence): # Direct inheritance
38+ def __init__(self): ... # Extra method not required by the ABC
39+ def __getitem__(self, index): ... # Required abstract method
40+ def __len__(self): ... # Required abstract method
41+ def count(self, value): ... # Optionally override a mixin method
4242
43- .. doctest ::
43+ .. doctest ::
4444
45- >>> issubclass (C, Sequence)
46- True
47- >>> isinstance (C(), Sequence)
48- True
45+ >>> issubclass (C, Sequence)
46+ True
47+ >>> isinstance (C(), Sequence)
48+ True
4949
50502) Existing classes and built-in classes can be registered as "virtual
51- subclasses" of the ABCs. Those classes should define the full API
52- including all of the abstract methods and all of the mixin methods.
53- This lets users rely on :func: `issubclass ` or :func: `isinstance ` tests
54- to determine whether the full interface is supported. The exception to
55- this rule is for methods that are automatically inferred from the rest
56- of the API:
51+ subclasses" of the ABCs. Those classes should define the full API
52+ including all of the abstract methods and all of the mixin methods.
53+ This lets users rely on :func: `issubclass ` or :func: `isinstance ` tests
54+ to determine whether the full interface is supported. The exception to
55+ this rule is for methods that are automatically inferred from the rest
56+ of the API:
5757
58- .. testcode ::
58+ .. testcode ::
5959
60- class D: # No inheritance
61- def __init__(self): ... # Extra method not required by the ABC
62- def __getitem__(self, index): ... # Abstract method
63- def __len__(self): ... # Abstract method
64- def count(self, value): ... # Mixin method
65- def index(self, value): ... # Mixin method
60+ class D: # No inheritance
61+ def __init__(self): ... # Extra method not required by the ABC
62+ def __getitem__(self, index): ... # Abstract method
63+ def __len__(self): ... # Abstract method
64+ def count(self, value): ... # Mixin method
65+ def index(self, value): ... # Mixin method
6666
67- Sequence.register(D) # Register instead of inherit
67+ Sequence.register(D) # Register instead of inherit
6868
69- .. doctest ::
69+ .. doctest ::
7070
71- >>> issubclass (D, Sequence)
72- True
73- >>> isinstance (D(), Sequence)
74- True
71+ >>> issubclass (D, Sequence)
72+ True
73+ >>> isinstance (D(), Sequence)
74+ True
7575
76- In this example, class :class: `!D ` does not need to define
77- ``__contains__ ``, ``__iter__ ``, and ``__reversed__ `` because the
78- :ref: `in-operator <comparisons >`, the :term: `iteration <iterable> `
79- logic, and the :func: `reversed ` function automatically fall back to
80- using ``__getitem__ `` and ``__len__ ``.
76+ In this example, class :class: `!D ` does not need to define
77+ ``__contains__ ``, ``__iter__ ``, and ``__reversed__ `` because the
78+ :ref: `in-operator <comparisons >`, the :term: `iteration <iterable> `
79+ logic, and the :func: `reversed ` function automatically fall back to
80+ using ``__getitem__ `` and ``__len__ ``.
8181
82823) Some simple interfaces are directly recognizable by the presence of
83- the required methods (unless those methods have been set to
84- :const: `None `):
83+ the required methods (unless those methods have been set to :const: `None `):
8584
86- .. testcode ::
85+ .. testcode ::
8786
88- class E:
89- def __iter__(self): ...
90- def __next__(self): ...
87+ class E:
88+ def __iter__(self): ...
89+ def __next__(self): ...
9190
92- .. doctest ::
91+ .. doctest ::
9392
94- >>> issubclass (E, Iterable)
95- True
96- >>> isinstance (E(), Iterable)
97- True
93+ >>> issubclass (E, Iterable)
94+ True
95+ >>> isinstance (E(), Iterable)
96+ True
9897
99- Complex interfaces do not support this last technique because an
100- interface is more than just the presence of method names. Interfaces
101- specify semantics and relationships between methods that cannot be
102- inferred solely from the presence of specific method names. For
103- example, knowing that a class supplies ``__getitem__ ``, ``__len__ ``, and
104- ``__iter__ `` is insufficient for distinguishing a :class: `Sequence ` from
105- a :class: `Mapping `.
98+ Complex interfaces do not support this last technique because an
99+ interface is more than just the presence of method names. Interfaces
100+ specify semantics and relationships between methods that cannot be
101+ inferred solely from the presence of specific method names. For
102+ example, knowing that a class supplies ``__getitem__ ``, ``__len__ ``, and
103+ ``__iter__ `` is insufficient for distinguishing a :class: `Sequence ` from
104+ a :class: `Mapping `.
106105
107106.. versionadded :: 3.9
108107 These abstract classes now support ``[] ``. See :ref: `types-genericalias `
0 commit comments