OverloadFunction Decorator
概述 / Overview
OverloadFunction 是一个 Python 装饰器,用于实现函数重载功能。它允许你为同一个函数名定义多个实现,根据传入参数的类型自动选择正确的函数版本。
OverloadFunction is a Python decorator that implements function overloading. It allows you to define multiple implementations for the same function name, automatically selecting the correct version based on the types of arguments passed.
功能特性 / Features
- 基于参数类型的函数重载 / Function overloading based on argument types
- 类型检查确保调用正确的函数版本 / Type checking ensures the correct function version is called
- 清晰的错误提示 / Clear error messages
- 支持详细的类型注解 / Supports detailed type annotations
安装 / Installation
pip install overload_function_1
使用示例 / Usage Example
from typing import Union
from overload_function import OverloadFunction
@OverloadFunction
def process_data(a: int, b: str) -> None:
print(f"Processing int {a} and string '{b}'")
@process_data.overload
def process_data(a: str, b: int) -> None:
print(f"Processing string '{a}' and int {b}")
@process_data.overload
def process_data(a: Union[int, float], b: Union[int, float]) -> None:
print(f"Processing numbers {a} and {b}")
# 正确调用 / Correct calls
process_data(1, "hello") # 调用第一个版本 / Calls first version
process_data("hello", 2) # 调用第二个版本 / Calls second version
process_data(3.14, 42) # 调用第三个版本 / Calls third version
# 错误调用 / Incorrect calls
process_data("hello", "world") # 抛出 TypeError / Raises TypeError
process_data([], {}) # 抛出 TypeError / Raises TypeError
注意事项 / Notes
-
不支持可变参数 / No support for variable arguments:
- 不能装饰包含
*args或**kwargs的函数 - Cannot decorate functions with
*argsor**kwargsparameters
- 不能装饰包含
-
类型注解推荐 / Type annotations recommended:
- 为获得最佳效果,请为函数参数添加类型注解
- For best results, add type annotations to function parameters
-
性能考虑 / Performance considerations:
- 每次调用都会进行类型检查,可能影响性能
- Type checking on each call may impact performance
-
明确性 / Explicitness:
- 当多个函数版本匹配时,会抛出异常
- Raises an exception when multiple function versions match
许可证 / License
MIT License
Metadata
Release files for overload-function-1 0.1.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 | Provenance | |
|---|---|---|---|---|
| overload_function_1-0.1.0.tar.gz | 4.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| overload_function_1-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.8 kB
Release files / overload_function_1-0.1.0.tar.gz
| Download URL | overload_function_1-0.1.0.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ea413cbc34bbd61f1adf25a8e6ebb7201b2de148658345915d3b05e505df5f2
|
|
BLAKE2b-256 checksum How to use checksums |
7f2574a994e8b1c3375d993b52df69c56f42b054a256b324f519b147e3248574
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Release files / overload_function_1-0.1.0-py3-none-any.whl
| Download URL | overload_function_1-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8fc93fe303fd067cb5d02462c2aa740bc789da0657a89b1347bf4acb5cb1dab1
|
|
BLAKE2b-256 checksum How to use checksums |
41568e2915517d316f6d35f0e90155259201b444fb2158ce5bd2a07dc81476b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.5
|
Security for overload-function-1 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.