@@ -28,81 +28,80 @@ An :func:`issubclass` or :func:`isinstance` test for an interface works in one
28
28
of three ways.
29
29
30
30
1) 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:
34
34
35
- .. testcode ::
35
+ .. testcode ::
36
36
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
42
42
43
- .. doctest ::
43
+ .. doctest ::
44
44
45
- >>> issubclass (C, Sequence)
46
- True
47
- >>> isinstance (C(), Sequence)
48
- True
45
+ >>> issubclass (C, Sequence)
46
+ True
47
+ >>> isinstance (C(), Sequence)
48
+ True
49
49
50
50
2) 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:
57
57
58
- .. testcode ::
58
+ .. testcode ::
59
59
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
66
66
67
- Sequence.register(D) # Register instead of inherit
67
+ Sequence.register(D) # Register instead of inherit
68
68
69
- .. doctest ::
69
+ .. doctest ::
70
70
71
- >>> issubclass (D, Sequence)
72
- True
73
- >>> isinstance (D(), Sequence)
74
- True
71
+ >>> issubclass (D, Sequence)
72
+ True
73
+ >>> isinstance (D(), Sequence)
74
+ True
75
75
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__ ``.
81
81
82
82
3) 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 `):
85
84
86
- .. testcode ::
85
+ .. testcode ::
87
86
88
- class E:
89
- def __iter__(self): ...
90
- def __next__(self): ...
87
+ class E:
88
+ def __iter__(self): ...
89
+ def __next__(self): ...
91
90
92
- .. doctest ::
91
+ .. doctest ::
93
92
94
- >>> issubclass (E, Iterable)
95
- True
96
- >>> isinstance (E(), Iterable)
97
- True
93
+ >>> issubclass (E, Iterable)
94
+ True
95
+ >>> isinstance (E(), Iterable)
96
+ True
98
97
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 `.
106
105
107
106
.. versionadded :: 3.9
108
107
These abstract classes now support ``[] ``. See :ref: `types-genericalias `
0 commit comments