plum-dispatch


Nameplum-dispatch JSON
Version 2.3.5 PyPI version JSON
download
home_pageNone
SummaryMultiple dispatch in Python
upload_time2024-04-21 15:12:22
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords multiple dispatch
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # [Plum: Multiple Dispatch in Python](https://github.com/beartype/plum)

[![DOI](https://zenodo.org/badge/110279931.svg)](https://zenodo.org/badge/latestdoi/110279931)
[![CI](https://github.com/beartype/plum/workflows/CI/badge.svg?branch=master)](https://github.com/beartype/plum/actions?query=workflow%3ACI)
[![Coverage Status](https://coveralls.io/repos/github/beartype/plum/badge.svg?branch=master&service=github)](https://coveralls.io/github/beartype/plum?branch=master)
[![Latest Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://beartype.github.io/plum)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

Everybody likes multiple dispatch, just like everybody likes plums.

The design philosophy of Plum is to provide an implementation of multiple dispatch that is Pythonic, yet close to how [Julia](http://julialang.org/) does it.
[See here for a comparison between Plum, `multipledispatch`, and `multimethod`.](https://beartype.github.io/plum/comparison.html)

*Note:*
Plum 2 is now powered by [Beartype](https://github.com/beartype/beartype)!
If you notice any issues with the new release, please open an issue.

# Installation

Plum requires Python 3.8 or higher.

```bash
pip install plum-dispatch
```

# [Documentation](https://beartype.github.io/plum)

See [here](https://beartype.github.io/plum).

# What's This?

Plum brings your type annotations to life:

```python
from numbers import Number

from plum import dispatch


@dispatch
def f(x: str):
    return "This is a string!"


@dispatch
def f(x: int):
    return "This is an integer!"


@dispatch
def f(x: Number):
    return "This is a general number, but I don't know which type."
```

```python
>>> f("1")
'This is a string!'

>>> f(1)
'This is an integer!'

>>> f(1.0)
'This is a number, but I don't know which type.'

>>> f(object())
NotFoundLookupError: `f(<object object at 0x7fd3b01cd330>)` could not be resolved.

Closest candidates are the following:
    f(x: str)
        <function f at 0x7fd400644ee0> @ /<ipython-input-2-c9f6cdbea9f3>:6
    f(x: int)
        <function f at 0x7fd3a0235ca0> @ /<ipython-input-2-c9f6cdbea9f3>:11
    f(x: numbers.Number)
        <function f at 0x7fd3a0235d30> @ /<ipython-input-2-c9f6cdbea9f3>:16
```

This also works for multiple arguments, enabling some neat design patterns:

```python
from numbers import Number, Real, Rational

from plum import dispatch


@dispatch
def multiply(x: Number, y: Number):
    return "Performing fallback implementation of multiplication..."


@dispatch
def multiply(x: Real, y: Real):
    return "Performing specialised implementation for reals..."


@dispatch
def multiply(x: Rational, y: Rational):
    return "Performing specialised implementation for rationals..."
```

```python
>>> multiply(1, 1)
'Performing specialised implementation for rationals...'

>>> multiply(1.0, 1.0)
'Performing specialised implementation for reals...'

>>> multiply(1j, 1j)
'Performing fallback implementation of multiplication...'

>>> multiply(1, 1.0)  # For mixed types, it automatically chooses the right optimisation!
'Performing specialised implementation for reals...'
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "plum-dispatch",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "multiple dispatch",
    "author": null,
    "author_email": "Wessel Bruinsma <wessel.p.bruinsma@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/7d/ce/c619ad4161bf38c4f3c9358fa02dca3ec3ce619b558a4cbe541dcbb2e800/plum_dispatch-2.3.5.tar.gz",
    "platform": null,
    "description": "# [Plum: Multiple Dispatch in Python](https://github.com/beartype/plum)\n\n[![DOI](https://zenodo.org/badge/110279931.svg)](https://zenodo.org/badge/latestdoi/110279931)\n[![CI](https://github.com/beartype/plum/workflows/CI/badge.svg?branch=master)](https://github.com/beartype/plum/actions?query=workflow%3ACI)\n[![Coverage Status](https://coveralls.io/repos/github/beartype/plum/badge.svg?branch=master&service=github)](https://coveralls.io/github/beartype/plum?branch=master)\n[![Latest Docs](https://img.shields.io/badge/docs-latest-blue.svg)](https://beartype.github.io/plum)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nEverybody likes multiple dispatch, just like everybody likes plums.\n\nThe design philosophy of Plum is to provide an implementation of multiple dispatch that is Pythonic, yet close to how [Julia](http://julialang.org/) does it.\n[See here for a comparison between Plum, `multipledispatch`, and `multimethod`.](https://beartype.github.io/plum/comparison.html)\n\n*Note:*\nPlum 2 is now powered by [Beartype](https://github.com/beartype/beartype)!\nIf you notice any issues with the new release, please open an issue.\n\n# Installation\n\nPlum requires Python 3.8 or higher.\n\n```bash\npip install plum-dispatch\n```\n\n# [Documentation](https://beartype.github.io/plum)\n\nSee [here](https://beartype.github.io/plum).\n\n# What's This?\n\nPlum brings your type annotations to life:\n\n```python\nfrom numbers import Number\n\nfrom plum import dispatch\n\n\n@dispatch\ndef f(x: str):\n    return \"This is a string!\"\n\n\n@dispatch\ndef f(x: int):\n    return \"This is an integer!\"\n\n\n@dispatch\ndef f(x: Number):\n    return \"This is a general number, but I don't know which type.\"\n```\n\n```python\n>>> f(\"1\")\n'This is a string!'\n\n>>> f(1)\n'This is an integer!'\n\n>>> f(1.0)\n'This is a number, but I don't know which type.'\n\n>>> f(object())\nNotFoundLookupError: `f(<object object at 0x7fd3b01cd330>)` could not be resolved.\n\nClosest candidates are the following:\n    f(x: str)\n        <function f at 0x7fd400644ee0> @ /<ipython-input-2-c9f6cdbea9f3>:6\n    f(x: int)\n        <function f at 0x7fd3a0235ca0> @ /<ipython-input-2-c9f6cdbea9f3>:11\n    f(x: numbers.Number)\n        <function f at 0x7fd3a0235d30> @ /<ipython-input-2-c9f6cdbea9f3>:16\n```\n\nThis also works for multiple arguments, enabling some neat design patterns:\n\n```python\nfrom numbers import Number, Real, Rational\n\nfrom plum import dispatch\n\n\n@dispatch\ndef multiply(x: Number, y: Number):\n    return \"Performing fallback implementation of multiplication...\"\n\n\n@dispatch\ndef multiply(x: Real, y: Real):\n    return \"Performing specialised implementation for reals...\"\n\n\n@dispatch\ndef multiply(x: Rational, y: Rational):\n    return \"Performing specialised implementation for rationals...\"\n```\n\n```python\n>>> multiply(1, 1)\n'Performing specialised implementation for rationals...'\n\n>>> multiply(1.0, 1.0)\n'Performing specialised implementation for reals...'\n\n>>> multiply(1j, 1j)\n'Performing fallback implementation of multiplication...'\n\n>>> multiply(1, 1.0)  # For mixed types, it automatically chooses the right optimisation!\n'Performing specialised implementation for reals...'\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Multiple dispatch in Python",
    "version": "2.3.5",
    "project_urls": {
        "repository": "https://github.com/beartype/plum"
    },
    "split_keywords": [
        "multiple",
        "dispatch"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7c46cf9984f7743239ae4179c14d5ea2d5ace1d7443843d4a1f939b41aa6550f",
                "md5": "1b912733a60d001c3e0bf3baac93dc89",
                "sha256": "4d1038455ad562e677a161ae9a28a4867af1bfc7c47cc7a97e2021b3043f14fd"
            },
            "downloads": -1,
            "filename": "plum_dispatch-2.3.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1b912733a60d001c3e0bf3baac93dc89",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 37942,
            "upload_time": "2024-04-21T15:12:20",
            "upload_time_iso_8601": "2024-04-21T15:12:20.025613Z",
            "url": "https://files.pythonhosted.org/packages/7c/46/cf9984f7743239ae4179c14d5ea2d5ace1d7443843d4a1f939b41aa6550f/plum_dispatch-2.3.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7dcec619ad4161bf38c4f3c9358fa02dca3ec3ce619b558a4cbe541dcbb2e800",
                "md5": "a784d9e044bbfc40e24fd7c9085fddbf",
                "sha256": "7ad89cc2029d87b0e3bacc7cc773f196ae329ce115f308b62f2d06c68b18a38d"
            },
            "downloads": -1,
            "filename": "plum_dispatch-2.3.5.tar.gz",
            "has_sig": false,
            "md5_digest": "a784d9e044bbfc40e24fd7c9085fddbf",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 30960,
            "upload_time": "2024-04-21T15:12:22",
            "upload_time_iso_8601": "2024-04-21T15:12:22.271873Z",
            "url": "https://files.pythonhosted.org/packages/7d/ce/c619ad4161bf38c4f3c9358fa02dca3ec3ce619b558a4cbe541dcbb2e800/plum_dispatch-2.3.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-21 15:12:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "beartype",
    "github_project": "plum",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "tox": true,
    "lcname": "plum-dispatch"
}
        
Elapsed time: 0.24751s