Backport of @functools.singledispatchmethod to Python 2.7-3.7.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file singledispatchmethod-1.0.tar.gz
.
File metadata
- Download URL: singledispatchmethod-1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 183a7fbeab53b9c9d182f8b8f9c2d7e109a7d40afaa30261d81dd8de68cd73bf |
|
MD5 | d2653ccefce26ff614741cd71c2f6ec6 |
|
BLAKE2b-256 | bee020ecc21453d7f052ee04d0d9b3305798e0f6afc619885c9aaee4ba86b25c |
File details
Details for the file singledispatchmethod-1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: singledispatchmethod-1.0-py2.py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed4a794e701cbe415f0df32b71dbdb96d3a415701795bcfb5ee694f8c12418db |
|
MD5 | d8a740dd5c8284414f4f0afc930dc4cc |
|
BLAKE2b-256 | c4aecb1b0901b332754245ce6ce900beef6e6c6a97b42e1e4eb78ab66379a6ba |