@@ -402,8 +402,8 @@ The :mod:`functools` module defines the following functions:
402
402
dispatch> ` :term: `generic function `.
403
403
404
404
To define a generic function, decorate it with the ``@singledispatch ``
405
- decorator. Note that the dispatch happens on the type of the first argument,
406
- create your function accordingly ::
405
+ decorator. When defining a function using `` @singledispatch ``, note that the
406
+ dispatch happens on the type of the first argument ::
407
407
408
408
>>> from functools import singledispatch
409
409
>>> @singledispatch
@@ -413,9 +413,9 @@ The :mod:`functools` module defines the following functions:
413
413
... print(arg)
414
414
415
415
To add overloaded implementations to the function, use the :func: `register `
416
- attribute of the generic function. It is a decorator. For functions
417
- annotated with types, the decorator will infer the type of the first
418
- argument automatically::
416
+ attribute of the generic function, which can be used as a decorator. For
417
+ functions annotated with types, the decorator will infer the type of the
418
+ first argument automatically::
419
419
420
420
>>> @fun.register
421
421
... def _(arg: int, verbose=False):
@@ -441,17 +441,17 @@ The :mod:`functools` module defines the following functions:
441
441
...
442
442
443
443
444
- To enable registering lambdas and pre-existing functions, the
445
- :func: `register ` attribute can be used in a functional form::
444
+ To enable registering :term: ` lambdas<lambda> ` and pre-existing functions,
445
+ the :func: `register ` attribute can also be used in a functional form::
446
446
447
447
>>> def nothing(arg, verbose=False):
448
448
... print("Nothing.")
449
449
...
450
450
>>> fun.register(type(None), nothing)
451
451
452
- The :func: `register ` attribute returns the undecorated function which
453
- enables decorator stacking, pickling, as well as creating unit tests for
454
- each variant independently::
452
+ The :func: `register ` attribute returns the undecorated function. This
453
+ enables decorator stacking, :mod: ` pickling<pickle> `, and the creation
454
+ of unit tests for each variant independently::
455
455
456
456
>>> @fun.register(float)
457
457
... @fun.register(Decimal)
@@ -486,11 +486,12 @@ The :mod:`functools` module defines the following functions:
486
486
Where there is no registered implementation for a specific type, its
487
487
method resolution order is used to find a more generic implementation.
488
488
The original function decorated with ``@singledispatch `` is registered
489
- for the base `` object ` ` type, which means it is used if no better
489
+ for the base :class: ` object ` type, which means it is used if no better
490
490
implementation is found.
491
491
492
- If an implementation registered to :term: `abstract base class `, virtual
493
- subclasses will be dispatched to that implementation::
492
+ If an implementation is registered to an :term: `abstract base class `,
493
+ virtual subclasses of the base class will be dispatched to that
494
+ implementation::
494
495
495
496
>>> from collections.abc import Mapping
496
497
>>> @fun.register
@@ -503,7 +504,7 @@ The :mod:`functools` module defines the following functions:
503
504
>>> fun({"a": "b"})
504
505
a => b
505
506
506
- To check which implementation will the generic function choose for
507
+ To check which implementation the generic function will choose for
507
508
a given type, use the ``dispatch() `` attribute::
508
509
509
510
>>> fun.dispatch(float)
@@ -526,7 +527,7 @@ The :mod:`functools` module defines the following functions:
526
527
.. versionadded :: 3.4
527
528
528
529
.. versionchanged :: 3.7
529
- The :func: `register ` attribute supports using type annotations.
530
+ The :func: `register ` attribute now supports using type annotations.
530
531
531
532
532
533
.. class :: singledispatchmethod(func)
@@ -535,8 +536,9 @@ The :mod:`functools` module defines the following functions:
535
536
dispatch> ` :term: `generic function `.
536
537
537
538
To define a generic method, decorate it with the ``@singledispatchmethod ``
538
- decorator. Note that the dispatch happens on the type of the first non-self
539
- or non-cls argument, create your function accordingly::
539
+ decorator. When defining a function using ``@singledispatchmethod ``, note
540
+ that the dispatch happens on the type of the first non-*self * or non-*cls *
541
+ argument::
540
542
541
543
class Negator:
542
544
@singledispatchmethod
@@ -552,9 +554,10 @@ The :mod:`functools` module defines the following functions:
552
554
return not arg
553
555
554
556
``@singledispatchmethod `` supports nesting with other decorators such as
555
- ``@classmethod ``. Note that to allow for ``dispatcher.register ``,
556
- ``singledispatchmethod `` must be the *outer most * decorator. Here is the
557
- ``Negator `` class with the ``neg `` methods being class bound::
557
+ :func: `@classmethod<classmethod> `. Note that to allow for
558
+ ``dispatcher.register ``, ``singledispatchmethod `` must be the *outer most *
559
+ decorator. Here is the ``Negator `` class with the ``neg `` methods bound to
560
+ the class, rather than an instance of the class::
558
561
559
562
class Negator:
560
563
@singledispatchmethod
@@ -572,8 +575,9 @@ The :mod:`functools` module defines the following functions:
572
575
def _(cls, arg: bool):
573
576
return not arg
574
577
575
- The same pattern can be used for other similar decorators: ``staticmethod ``,
576
- ``abstractmethod ``, and others.
578
+ The same pattern can be used for other similar decorators:
579
+ :func: `@staticmethod<staticmethod> `,
580
+ :func: `@abstractmethod<abc.abstractmethod> `, and others.
577
581
578
582
.. versionadded :: 3.8
579
583
0 commit comments