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?
.. code:: bash
$ 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:
.. code:: python
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:
.. code:: python
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.
.. [1] https://docs.python.org/3.8/library/functools.html#functools.singledispatchmethod
.. [2] https://docs.python.org/3.8/glossary.html#term-single-dispatch
.. [3] https://docs.python.org/3.8/glossary.html#term-generic-function
Raw data
{
"_id": null,
"home_page": "https://github.com/ikalnytskyi/singledispatchmethod",
"name": "singledispatchmethod",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "",
"author": "Ihor Kalnytskyi",
"author_email": "ihor@kalnytskyi.com",
"download_url": "https://files.pythonhosted.org/packages/be/e0/20ecc21453d7f052ee04d0d9b3305798e0f6afc619885c9aaee4ba86b25c/singledispatchmethod-1.0.tar.gz",
"platform": "",
"description": "singledispatchmethod\n====================\n\nBackport of ``@functools.singledispatchmethod`` decorator [1]_ from\nPython 3.8 to Python 2.7-3.7. These are merely ~30 lines of code, but\nwhy bother yourself with copypasta?\n\n.. code:: bash\n\n $ pip install singledispatchmethod\n\nThe decorator transforms a method into a single-dispatch [2]_ generic\nfunction [3]_. Note that since the dispatch happens on the type of the\nfirst non-self or non-cls argument, you have to create your function\naccordingly:\n\n.. code:: python\n\n from singledispatchmethod import singledispatchmethod\n\n class Negator:\n\n @singledispatchmethod\n def neg(self, arg):\n raise NotImplementedError(\"Cannot negate a\")\n\n @neg.register\n def _(self, arg: int):\n return -arg\n\n @neg.register\n def _(self, arg: bool):\n return not arg\n\n``@singledispatchmethod`` supports nesting with other decorators such as\n``@classmethod``. However, in order to expose ``dispatcher.register``,\n``@singledispatchmethod`` must be the *outer most* decorator. Here is\nthe ``Negator`` class with the ``neg`` methods being class bound:\n\n.. code:: python\n\n from singledispatchmethod import singledispatchmethod\n\n class Negator:\n\n @singledispatchmethod\n @classmethod\n def neg(cls, arg):\n raise NotImplementedError(\"Cannot negate a\")\n\n @neg.register\n @classmethod\n def _(cls, arg: int):\n return -arg\n\n @neg.register\n @classmethod\n def _(cls, arg: bool):\n return not arg\n\nThe same pattern can be used for other similar decorators, such as\n``@staticmethod`` or ``@abstractmethod``. Please note, since\n``@singledispatchmethod`` decorator is based on\n``@functools.singledispatch``, type annotations are supported by\n``dispatcher.register`` only since Python 3.7.\n\n.. [1] https://docs.python.org/3.8/library/functools.html#functools.singledispatchmethod\n.. [2] https://docs.python.org/3.8/glossary.html#term-single-dispatch\n.. [3] https://docs.python.org/3.8/glossary.html#term-generic-function",
"bugtrack_url": null,
"license": "MIT",
"summary": "Backport of @functools.singledispatchmethod to Python 2.7-3.7.",
"version": "1.0",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "d8a740dd5c8284414f4f0afc930dc4cc",
"sha256": "ed4a794e701cbe415f0df32b71dbdb96d3a415701795bcfb5ee694f8c12418db"
},
"downloads": -1,
"filename": "singledispatchmethod-1.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "d8a740dd5c8284414f4f0afc930dc4cc",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 4674,
"upload_time": "2021-09-30T13:17:20",
"upload_time_iso_8601": "2021-09-30T13:17:20.270743Z",
"url": "https://files.pythonhosted.org/packages/c4/ae/cb1b0901b332754245ce6ce900beef6e6c6a97b42e1e4eb78ab66379a6ba/singledispatchmethod-1.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "d2653ccefce26ff614741cd71c2f6ec6",
"sha256": "183a7fbeab53b9c9d182f8b8f9c2d7e109a7d40afaa30261d81dd8de68cd73bf"
},
"downloads": -1,
"filename": "singledispatchmethod-1.0.tar.gz",
"has_sig": false,
"md5_digest": "d2653ccefce26ff614741cd71c2f6ec6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6064,
"upload_time": "2019-08-12T07:28:17",
"upload_time_iso_8601": "2019-08-12T07:28:17.216342Z",
"url": "https://files.pythonhosted.org/packages/be/e0/20ecc21453d7f052ee04d0d9b3305798e0f6afc619885c9aaee4ba86b25c/singledispatchmethod-1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-08-12 07:28:17",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "ikalnytskyi",
"github_project": "singledispatchmethod",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "singledispatchmethod"
}