1 file was added to this release more than 14 days after its initial publication. Inspect the release files before installing.
singledispatchmethod
Backport of @functools.singledispatchmethod decorator [1] from Python 3.8 to Python 2.7-3.7. These are merely ~30 lines of code, but why bother yourself with copypasta?
$ pip install singledispatchmethod
The decorator transforms a method into a single-dispatch [2] generic function [3]. Note that since the dispatch happens on the type of the first non-self or non-cls argument, you have to create your function accordingly:
from singledispatchmethod import singledispatchmethod
class Negator:
@singledispatchmethod
def neg(self, arg):
raise NotImplementedError("Cannot negate a")
@neg.register
def _(self, arg: int):
return -arg
@neg.register
def _(self, arg: bool):
return not arg
@singledispatchmethod supports nesting with other decorators such as @classmethod. However, in order to expose dispatcher.register, @singledispatchmethod must be the outer most decorator. Here is the Negator class with the neg methods being class bound:
from singledispatchmethod import singledispatchmethod
class Negator:
@singledispatchmethod
@classmethod
def neg(cls, arg):
raise NotImplementedError("Cannot negate a")
@neg.register
@classmethod
def _(cls, arg: int):
return -arg
@neg.register
@classmethod
def _(cls, arg: bool):
return not arg
The same pattern can be used for other similar decorators, such as @staticmethod or @abstractmethod. Please note, since @singledispatchmethod decorator is based on @functools.singledispatch, type annotations are supported by dispatcher.register only since Python 3.7.
Release files for singledispatchmethod 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
File added late
1 file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release files before installing.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| singledispatchmethod-1.0.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| singledispatchmethod-1.0-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size:10.7 kB
Release files / singledispatchmethod-1.0.tar.gz
| Download URL | singledispatchmethod-1.0.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
183a7fbeab53b9c9d182f8b8f9c2d7e109a7d40afaa30261d81dd8de68cd73bf
|
|
BLAKE2b-256 checksum How to use checksums |
bee020ecc21453d7f052ee04d0d9b3305798e0f6afc619885c9aaee4ba86b25c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.1
|
Release files / singledispatchmethod-1.0-py2.py3-none-any.whl
File added late
This file was uploaded more than 14 days after the first file in this release.
While project maintainers occasionally add legitimate files to an existing release, late additions can also indicate a security compromise.
We recommend inspecting the release file before installing.
| Download URL | singledispatchmethod-1.0-py2.py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ed4a794e701cbe415f0df32b71dbdb96d3a415701795bcfb5ee694f8c12418db
|
|
BLAKE2b-256 checksum How to use checksums |
c4aecb1b0901b332754245ce6ce900beef6e6c6a97b42e1e4eb78ab66379a6ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
|