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
Metadata
Release files for dynamic-overload 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dynamic_overload-0.2.0.tar.gz | 8.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dynamic_overload-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.6 kB
Release files / dynamic_overload-0.2.0.tar.gz
| Download URL | dynamic_overload-0.2.0.tar.gz |
|---|---|
| Size | 8.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f095ae06b220842643481b61a0fb018ea1dbe7e48d7de70cacb292d8a9aa9de9
|
|
BLAKE2b-256 checksum How to use checksums |
11db7f6822c1a56f077b13a2190aa8828beadda48eb07b47e798f4d55a3c3f85
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.0
|
Release files / dynamic_overload-0.2.0-py3-none-any.whl
| Download URL | dynamic_overload-0.2.0-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a032bef0dfbbf33c5ec788de61c4d64066e8caa3f45cc552626a09577c1819cb
|
|
BLAKE2b-256 checksum How to use checksums |
ec735e674e038e93f32181fbdf646842953a897aa0cf4aa1e4b7dc7d1d73538d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.0
|