Skip to main content

A basic package that allows to overload functions and class functions dynamically depending on the input types.

Project description

Dynamic Overload

Dynamic Overload is a python package that allows you to overload functions and classes using dynamic dispatch depending on the type of the arguments. This allows you to write more readable code and avoid using isinstance and type checks.

The package also detects collisions between the overloads and warns the user about them without breaking the code. In case of collision, the first overload is used. So, its recomended that you put the most specific overloads first before the more general ones.

Installation

Requires python 3.10 or higher

pip install dynamic-overload

Usage

For functions:

from dynamic_overload import overload
from typing import Iterable

@overload
def flatten(x: Iterable[int]) -> Iterable[int]:
    return [y for l in x for y in flatten(l)]

@overload
def flatten(x: int) -> Iterable[int]:
    yield x

assert list(flatten([1, [2, 3], 4])) == [1, 2, 3, 4]
assert list(flatten(1)) == [1]

For classes:

from dynamic_overload import OverloadMeta

class Integer(metaclass=OverloadMeta):
    x: int

    def __init__(self, x: int):
        self.x = x

    def __init__(self, x: str):
        self.x = int(x)

    def __init__(self, x: float):
        self.x = int(x)

assert Integer(1).x == 1
assert Integer('1').x == 1
assert Integer(1.0).x == 1

The package also supports inheritance similar to python abc module. So the previous example can be rewritten as:

from dynamic_overload import Overload

class Integer(Overload):
    x: int

    def __init__(self, x: int):
        self.x = x

    def __init__(self, x: str):
        self.x = int(x)

    def __init__(self, x: float):
        self.x = int(x)

assert Integer(1).x == 1
assert Integer('1').x == 1
assert Integer(1.0).x == 1

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dynamic_overload-0.2.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

dynamic_overload-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file dynamic_overload-0.2.0.tar.gz.

File metadata

  • Download URL: dynamic_overload-0.2.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.0

File hashes

Hashes for dynamic_overload-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f095ae06b220842643481b61a0fb018ea1dbe7e48d7de70cacb292d8a9aa9de9
MD5 67c8bf4618c858e4bbad7cccc19284f0
BLAKE2b-256 11db7f6822c1a56f077b13a2190aa8828beadda48eb07b47e798f4d55a3c3f85

See more details on using hashes here.

File details

Details for the file dynamic_overload-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dynamic_overload-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a032bef0dfbbf33c5ec788de61c4d64066e8caa3f45cc552626a09577c1819cb
MD5 26960464589884a4ac5ea0b4292dda0d
BLAKE2b-256 ec735e674e038e93f32181fbdf646842953a897aa0cf4aa1e4b7dc7d1d73538d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page