A Python decorator for function overloading based on argument types
Project description
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
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 overload_function_1-0.1.0.tar.gz.
File metadata
- Download URL: overload_function_1-0.1.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ea413cbc34bbd61f1adf25a8e6ebb7201b2de148658345915d3b05e505df5f2
|
|
| MD5 |
ec98f330c6424e1c20d7f6e691dbfd79
|
|
| BLAKE2b-256 |
7f2574a994e8b1c3375d993b52df69c56f42b054a256b324f519b147e3248574
|
File details
Details for the file overload_function_1-0.1.0-py3-none-any.whl.
File metadata
- Download URL: overload_function_1-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fc93fe303fd067cb5d02462c2aa740bc789da0657a89b1347bf4acb5cb1dab1
|
|
| MD5 |
30958e2b6a259718b0c112ef3b6803d2
|
|
| BLAKE2b-256 |
41568e2915517d316f6d35f0e90155259201b444fb2158ce5bd2a07dc81476b2
|