22
22
23
23
This module provides :term: `abstract base classes <abstract base class> ` that
24
24
can be used to test whether a class provides a particular interface; for
25
- example, whether it is :term: `hashable ` or whether it is a mapping.
25
+ example, whether it is :term: `hashable ` or whether it is a :term: ` mapping ` .
26
26
27
27
An :func: `issubclass ` or :func: `isinstance ` test for an interface works in one
28
28
of three ways.
@@ -73,7 +73,7 @@ of the API:
73
73
>>> isinstance (D(), Sequence)
74
74
True
75
75
76
- In this example, class :class: `D ` does not need to define
76
+ In this example, class :class: `! D ` does not need to define
77
77
``__contains__ ``, ``__iter__ ``, and ``__reversed__ `` because the
78
78
:ref: `in-operator <comparisons >`, the :term: `iteration <iterable> `
79
79
logic, and the :func: `reversed ` function automatically fall back to
@@ -183,14 +183,14 @@ ABC Inherits from Abstract Methods Mi
183
183
184
184
.. rubric :: Footnotes
185
185
186
- .. [1 ] These ABCs override :meth: `object .__subclasshook__ ` to support
186
+ .. [1 ] These ABCs override :meth: `~abc.ABCMeta .__subclasshook__ ` to support
187
187
testing an interface by verifying the required methods are present
188
188
and have not been set to :const: `None `. This only works for simple
189
189
interfaces. More complex interfaces require registration or direct
190
190
subclassing.
191
191
192
192
.. [2 ] Checking ``isinstance(obj, Iterable) `` detects classes that are
193
- registered as :class: `Iterable ` or that have an :meth: `__iter__ `
193
+ registered as :class: `Iterable ` or that have an :meth: `~container. __iter__ `
194
194
method, but it does not detect classes that iterate with the
195
195
:meth: `~object.__getitem__ ` method. The only reliable way to determine
196
196
whether an object is :term: `iterable ` is to call ``iter(obj) ``.
@@ -202,26 +202,27 @@ Collections Abstract Base Classes -- Detailed Descriptions
202
202
203
203
.. class :: Container
204
204
205
- ABC for classes that provide the :meth: `__contains__ ` method.
205
+ ABC for classes that provide the :meth: `~object. __contains__ ` method.
206
206
207
207
.. class :: Hashable
208
208
209
- ABC for classes that provide the :meth: `__hash__ ` method.
209
+ ABC for classes that provide the :meth: `~object. __hash__ ` method.
210
210
211
211
.. class :: Sized
212
212
213
- ABC for classes that provide the :meth: `__len__ ` method.
213
+ ABC for classes that provide the :meth: `~object. __len__ ` method.
214
214
215
215
.. class :: Callable
216
216
217
- ABC for classes that provide the :meth: `__call__ ` method.
217
+ ABC for classes that provide the :meth: `~object. __call__ ` method.
218
218
219
219
.. class :: Iterable
220
220
221
- ABC for classes that provide the :meth: `__iter__ ` method.
221
+ ABC for classes that provide the :meth: `~container. __iter__ ` method.
222
222
223
223
Checking ``isinstance(obj, Iterable) `` detects classes that are registered
224
- as :class: `Iterable ` or that have an :meth: `__iter__ ` method, but it does
224
+ as :class: `Iterable ` or that have an :meth: `~container.__iter__ ` method,
225
+ but it does
225
226
not detect classes that iterate with the :meth: `~object.__getitem__ ` method.
226
227
The only reliable way to determine whether an object is :term: `iterable `
227
228
is to call ``iter(obj) ``.
@@ -240,17 +241,17 @@ Collections Abstract Base Classes -- Detailed Descriptions
240
241
241
242
.. class :: Reversible
242
243
243
- ABC for iterable classes that also provide the :meth: `__reversed__ `
244
+ ABC for iterable classes that also provide the :meth: `~object. __reversed__ `
244
245
method.
245
246
246
247
.. versionadded :: 3.6
247
248
248
249
.. class :: Generator
249
250
250
- ABC for generator classes that implement the protocol defined in
251
- :pep: `342 ` that extends iterators with the :meth: `~generator.send `,
251
+ ABC for :term: `generator ` classes that implement the protocol defined in
252
+ :pep: `342 ` that extends :term: `iterators <iterator> ` with the
253
+ :meth: `~generator.send `,
252
254
:meth: `~generator.throw ` and :meth: `~generator.close ` methods.
253
- See also the definition of :term: `generator `.
254
255
255
256
.. versionadded :: 3.5
256
257
@@ -261,7 +262,7 @@ Collections Abstract Base Classes -- Detailed Descriptions
261
262
ABCs for read-only and mutable :term: `sequences <sequence> `.
262
263
263
264
Implementation note: Some of the mixin methods, such as
264
- :meth: `__iter__ `, :meth: `__reversed__ ` and :meth: `index `, make
265
+ :meth: `~container. __iter__ `, :meth: `~object. __reversed__ ` and :meth: `index `, make
265
266
repeated calls to the underlying :meth: `~object.__getitem__ ` method.
266
267
Consequently, if :meth: `~object.__getitem__ ` is implemented with constant
267
268
access speed, the mixin methods will have linear performance;
@@ -282,7 +283,7 @@ Collections Abstract Base Classes -- Detailed Descriptions
282
283
.. class :: Set
283
284
MutableSet
284
285
285
- ABCs for read-only and mutable sets.
286
+ ABCs for read-only and mutable :ref: ` sets < types-set >` .
286
287
287
288
.. class :: Mapping
288
289
MutableMapping
@@ -299,42 +300,42 @@ Collections Abstract Base Classes -- Detailed Descriptions
299
300
.. class :: Awaitable
300
301
301
302
ABC for :term: `awaitable ` objects, which can be used in :keyword: `await `
302
- expressions. Custom implementations must provide the :meth: ` __await__ `
303
- method.
303
+ expressions. Custom implementations must provide the
304
+ :meth: ` ~object.__await__ ` method.
304
305
305
306
:term: `Coroutine <coroutine> ` objects and instances of the
306
307
:class: `~collections.abc.Coroutine ` ABC are all instances of this ABC.
307
308
308
309
.. note ::
309
- In CPython, generator-based coroutines (generators decorated with
310
- :func: `types.coroutine `) are
311
- *awaitables *, even though they do not have an :meth: `__await__ ` method.
310
+ In CPython, generator-based coroutines (:term: ` generators <generator> `
311
+ decorated with :func: `@ types.coroutine <types.coroutine> `) are
312
+ *awaitables *, even though they do not have an :meth: `~object. __await__ ` method.
312
313
Using ``isinstance(gencoro, Awaitable) `` for them will return ``False ``.
313
314
Use :func: `inspect.isawaitable ` to detect them.
314
315
315
316
.. versionadded :: 3.5
316
317
317
318
.. class :: Coroutine
318
319
319
- ABC for coroutine compatible classes. These implement the
320
+ ABC for :term: ` coroutine ` compatible classes. These implement the
320
321
following methods, defined in :ref: `coroutine-objects `:
321
322
:meth: `~coroutine.send `, :meth: `~coroutine.throw `, and
322
323
:meth: `~coroutine.close `. Custom implementations must also implement
323
- :meth: `__await__ `. All :class: `Coroutine ` instances are also instances of
324
- :class: ` Awaitable `. See also the definition of :term: ` coroutine `.
324
+ :meth: `~object. __await__ `. All :class: `Coroutine ` instances are also
325
+ instances of :class: ` Awaitable `.
325
326
326
327
.. note ::
327
- In CPython, generator-based coroutines (generators decorated with
328
- :func: `types.coroutine `) are
329
- *awaitables *, even though they do not have an :meth: `__await__ ` method.
328
+ In CPython, generator-based coroutines (:term: ` generators <generator> `
329
+ decorated with :func: `@ types.coroutine <types.coroutine> `) are
330
+ *awaitables *, even though they do not have an :meth: `~object. __await__ ` method.
330
331
Using ``isinstance(gencoro, Coroutine) `` for them will return ``False ``.
331
332
Use :func: `inspect.isawaitable ` to detect them.
332
333
333
334
.. versionadded :: 3.5
334
335
335
336
.. class :: AsyncIterable
336
337
337
- ABC for classes that provide ``__aiter__ `` method. See also the
338
+ ABC for classes that provide an ``__aiter__ `` method. See also the
338
339
definition of :term: `asynchronous iterable `.
339
340
340
341
.. versionadded :: 3.5
@@ -348,7 +349,7 @@ Collections Abstract Base Classes -- Detailed Descriptions
348
349
349
350
.. class :: AsyncGenerator
350
351
351
- ABC for asynchronous generator classes that implement the protocol
352
+ ABC for :term: ` asynchronous generator ` classes that implement the protocol
352
353
defined in :pep: `525 ` and :pep: `492 `.
353
354
354
355
.. versionadded :: 3.6
@@ -373,9 +374,9 @@ particular functionality, for example::
373
374
Several of the ABCs are also useful as mixins that make it easier to develop
374
375
classes supporting container APIs. For example, to write a class supporting
375
376
the full :class: `Set ` API, it is only necessary to supply the three underlying
376
- abstract methods: :meth: `__contains__ `, :meth: `__iter__ `, and :meth: ` __len__ `.
377
- The ABC supplies the remaining methods such as :meth: ` __and__ ` and
378
- :meth: `isdisjoint `::
377
+ abstract methods: :meth: `~object. __contains__ `, :meth: `~container. __iter__ `, and
378
+ :meth: ` ~object.__len__ `. The ABC supplies the remaining methods such as
379
+ :meth: `!__and__ ` and :meth: ` ~frozenset. isdisjoint `::
379
380
380
381
class ListBasedSet(collections.abc.Set):
381
382
''' Alternate set implementation favoring space over speed
@@ -403,23 +404,24 @@ Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
403
404
404
405
(1)
405
406
Since some set operations create new sets, the default mixin methods need
406
- a way to create new instances from an iterable. The class constructor is
407
+ a way to create new instances from an :term: ` iterable ` . The class constructor is
407
408
assumed to have a signature in the form ``ClassName(iterable) ``.
408
- That assumption is factored-out to an internal classmethod called
409
- :meth: `_from_iterable ` which calls ``cls(iterable) `` to produce a new set.
409
+ That assumption is factored-out to an internal :class: ` classmethod ` called
410
+ :meth: `! _from_iterable ` which calls ``cls(iterable) `` to produce a new set.
410
411
If the :class: `Set ` mixin is being used in a class with a different
411
- constructor signature, you will need to override :meth: `_from_iterable `
412
+ constructor signature, you will need to override :meth: `! _from_iterable `
412
413
with a classmethod or regular method that can construct new instances from
413
414
an iterable argument.
414
415
415
416
(2)
416
417
To override the comparisons (presumably for speed, as the
417
- semantics are fixed), redefine :meth: `__le__ ` and :meth: `__ge__ `,
418
+ semantics are fixed), redefine :meth: `~object.__le__ ` and
419
+ :meth: `~object.__ge__ `,
418
420
then the other operations will automatically follow suit.
419
421
420
422
(3)
421
- The :class: `Set ` mixin provides a :meth: `_hash ` method to compute a hash value
422
- for the set; however, :meth: `__hash__ ` is not defined because not all sets
423
+ The :class: `Set ` mixin provides a :meth: `! _hash ` method to compute a hash value
424
+ for the set; however, :meth: `~object. __hash__ ` is not defined because not all sets
423
425
are :term: `hashable ` or immutable. To add set hashability using mixins,
424
426
inherit from both :meth: `Set ` and :meth: `Hashable `, then define
425
427
``__hash__ = Set._hash ``.
0 commit comments