Library to enable function overloading in Python
Project description
Introduction
This library is enables method overloading in Python taking care of the method resolution without additional user required code given sufficient typehints on the various functions. It brings no additional requirements and can be easily integrated into existing codebases.
License and Copyright Information
This library is distributed under the MIT License, see LICENSE.
Installation
This project has no dependencies and can be installed with pip either locally
git clone https://github.com/steinmig/polymethod.git
pip install ./polymethod
or from PyPI
pip install polymethod
Building documentation
The dependencies for building the documentation can be installed with
pip install -r docs/requirements.txt
The documentation can then be built and opened with
sphinx-build docs/source docs/build
firefox docs/build/index.html
Example Usage
This package allows you to overload functions, i.e. give multiple functions the same name, but let them require different number and/or types of arguments. This allows to provide simple APIs that can handle various inputs. Currently, the overloading feature is limited to methods, i.e. functions of a class. The overloaded methods can be regular methods, classmethods, or staticmethods. An example class would be
from polymethod import overload, OverloadMeta, NoMatchingOverload
from typing import Tuple
class API(metaclass=OverloadMeta):
@classmethod
@overload
def provide_id(cls, user_name: str) -> int:
# some function body
return 0
@classmethod
@overload
def provide_id(cls, birthday: Tuple[int, int, int]) -> int:
# another function body
return 1
assert API.provide_id("A user name") == 0
assert API.provide_id((4, 7, 1776)) == 1
try:
API.provide_id(0)
except NoMatchingOverload as e:
print(str(e))
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file polymethod-1.0.0.tar.gz.
File metadata
- Download URL: polymethod-1.0.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a155ed3ba62be344cef13b328fa529f3ab1f02b0476bd8d15a5bd433fbe94c8
|
|
| MD5 |
d839368bde884e38968a48032cc0f564
|
|
| BLAKE2b-256 |
ed176c6d7a0121b9066ec493bd2de99667780ae1a5a04ac4769e20dda68a1011
|
File details
Details for the file polymethod-1.0.0-py3-none-any.whl.
File metadata
- Download URL: polymethod-1.0.0-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8617695be6d20bad09066dd8410a5a2ff927113d2374dc65cca4b243acb203b
|
|
| MD5 |
866a033a24d159fab787d6cc8db52da3
|
|
| BLAKE2b-256 |
cc348931e9a8c301df2c0e9bd3b5560811b47b203257eee0cb7083ac9ea0a647
|