@@ -392,8 +392,8 @@ The :mod:`functools` module defines the following functions:
392
392
dispatch> ` :term: `generic function `.
393
393
394
394
To define a generic function, decorate it with the ``@singledispatch ``
395
- decorator. Note that the dispatch happens on the type of the first argument,
396
- create your function accordingly ::
395
+ decorator. When defining a function using `` @singledispatch ``, note that the
396
+ dispatch happens on the type of the first argument ::
397
397
398
398
>>> from functools import singledispatch
399
399
>>> @singledispatch
@@ -403,9 +403,9 @@ The :mod:`functools` module defines the following functions:
403
403
... print(arg)
404
404
405
405
To add overloaded implementations to the function, use the :func: `register `
406
- attribute of the generic function. It is a decorator. For functions
407
- annotated with types, the decorator will infer the type of the first
408
- argument automatically::
406
+ attribute of the generic function, which can be used as a decorator. For
407
+ functions annotated with types, the decorator will infer the type of the
408
+ first argument automatically::
409
409
410
410
>>> @fun.register
411
411
... def _(arg: int, verbose=False):
@@ -431,17 +431,17 @@ The :mod:`functools` module defines the following functions:
431
431
...
432
432
433
433
434
- To enable registering lambdas and pre-existing functions, the
435
- :func: `register ` attribute can be used in a functional form::
434
+ To enable registering :term: ` lambdas<lambda> ` and pre-existing functions,
435
+ the :func: `register ` attribute can also be used in a functional form::
436
436
437
437
>>> def nothing(arg, verbose=False):
438
438
... print("Nothing.")
439
439
...
440
440
>>> fun.register(type(None), nothing)
441
441
442
- The :func: `register ` attribute returns the undecorated function which
443
- enables decorator stacking, pickling, as well as creating unit tests for
444
- each variant independently::
442
+ The :func: `register ` attribute returns the undecorated function. This
443
+ enables decorator stacking, :mod: ` pickling<pickle> `, and the creation
444
+ of unit tests for each variant independently::
445
445
446
446
>>> @fun.register(float)
447
447
... @fun.register(Decimal)
@@ -476,11 +476,12 @@ The :mod:`functools` module defines the following functions:
476
476
Where there is no registered implementation for a specific type, its
477
477
method resolution order is used to find a more generic implementation.
478
478
The original function decorated with ``@singledispatch `` is registered
479
- for the base `` object ` ` type, which means it is used if no better
479
+ for the base :class: ` object ` type, which means it is used if no better
480
480
implementation is found.
481
481
482
- If an implementation registered to :term: `abstract base class `, virtual
483
- subclasses will be dispatched to that implementation::
482
+ If an implementation is registered to an :term: `abstract base class `,
483
+ virtual subclasses of the base class will be dispatched to that
484
+ implementation::
484
485
485
486
>>> from collections.abc import Mapping
486
487
>>> @fun.register
@@ -493,7 +494,7 @@ The :mod:`functools` module defines the following functions:
493
494
>>> fun({"a": "b"})
494
495
a => b
495
496
496
- To check which implementation will the generic function choose for
497
+ To check which implementation the generic function will choose for
497
498
a given type, use the ``dispatch() `` attribute::
498
499
499
500
>>> fun.dispatch(float)
@@ -516,7 +517,7 @@ The :mod:`functools` module defines the following functions:
516
517
.. versionadded :: 3.4
517
518
518
519
.. versionchanged :: 3.7
519
- The :func: `register ` attribute supports using type annotations.
520
+ The :func: `register ` attribute now supports using type annotations.
520
521
521
522
522
523
.. class :: singledispatchmethod(func)
@@ -525,8 +526,9 @@ The :mod:`functools` module defines the following functions:
525
526
dispatch> ` :term: `generic function `.
526
527
527
528
To define a generic method, decorate it with the ``@singledispatchmethod ``
528
- decorator. Note that the dispatch happens on the type of the first non-self
529
- or non-cls argument, create your function accordingly::
529
+ decorator. When defining a function using ``@singledispatchmethod ``, note
530
+ that the dispatch happens on the type of the first non-*self * or non-*cls *
531
+ argument::
530
532
531
533
class Negator:
532
534
@singledispatchmethod
@@ -542,9 +544,10 @@ The :mod:`functools` module defines the following functions:
542
544
return not arg
543
545
544
546
``@singledispatchmethod `` supports nesting with other decorators such as
545
- ``@classmethod ``. Note that to allow for ``dispatcher.register ``,
546
- ``singledispatchmethod `` must be the *outer most * decorator. Here is the
547
- ``Negator `` class with the ``neg `` methods being class bound::
547
+ :func: `@classmethod<classmethod> `. Note that to allow for
548
+ ``dispatcher.register ``, ``singledispatchmethod `` must be the *outer most *
549
+ decorator. Here is the ``Negator `` class with the ``neg `` methods bound to
550
+ the class, rather than an instance of the class::
548
551
549
552
class Negator:
550
553
@singledispatchmethod
@@ -562,8 +565,9 @@ The :mod:`functools` module defines the following functions:
562
565
def _(cls, arg: bool):
563
566
return not arg
564
567
565
- The same pattern can be used for other similar decorators: ``staticmethod ``,
566
- ``abstractmethod ``, and others.
568
+ The same pattern can be used for other similar decorators:
569
+ :func: `@staticmethod<staticmethod> `,
570
+ :func: `@abstractmethod<abc.abstractmethod> `, and others.
567
571
568
572
.. versionadded :: 3.8
569
573
0 commit comments