# 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
```bash
pip install overload_function_1
```
## 使用示例 / Usage Example
```python
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
1. **不支持可变参数** / **No support for variable arguments**:
- 不能装饰包含 `*args` 或 `**kwargs` 的函数
- Cannot decorate functions with `*args` or `**kwargs` parameters
2. **类型注解推荐** / **Type annotations recommended**:
- 为获得最佳效果,请为函数参数添加类型注解
- For best results, add type annotations to function parameters
3. **性能考虑** / **Performance considerations**:
- 每次调用都会进行类型检查,可能影响性能
- Type checking on each call may impact performance
4. **明确性** / **Explicitness**:
- 当多个函数版本匹配时,会抛出异常
- Raises an exception when multiple function versions match
## 许可证 / License
MIT License
Raw data
{
"_id": null,
"home_page": "https://github.com/Locked-chess-official/overload-function",
"name": "overload-function-1",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "overload function decorator type checking",
"author": "HuangHaoHUa",
"author_email": "13140752715@example.com",
"download_url": "https://files.pythonhosted.org/packages/7f/25/74a994e8b1c3375d993b52df69c56f42b054a256b324f519b147e3248574/overload_function_1-0.1.0.tar.gz",
"platform": null,
"description": "# OverloadFunction Decorator\r\n\r\n## \u6982\u8ff0 / Overview\r\n\r\n`OverloadFunction` \u662f\u4e00\u4e2a Python \u88c5\u9970\u5668\uff0c\u7528\u4e8e\u5b9e\u73b0\u51fd\u6570\u91cd\u8f7d\u529f\u80fd\u3002\u5b83\u5141\u8bb8\u4f60\u4e3a\u540c\u4e00\u4e2a\u51fd\u6570\u540d\u5b9a\u4e49\u591a\u4e2a\u5b9e\u73b0\uff0c\u6839\u636e\u4f20\u5165\u53c2\u6570\u7684\u7c7b\u578b\u81ea\u52a8\u9009\u62e9\u6b63\u786e\u7684\u51fd\u6570\u7248\u672c\u3002\r\n\r\n`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.\r\n\r\n## \u529f\u80fd\u7279\u6027 / Features\r\n\r\n- \u57fa\u4e8e\u53c2\u6570\u7c7b\u578b\u7684\u51fd\u6570\u91cd\u8f7d / Function overloading based on argument types\r\n- \u7c7b\u578b\u68c0\u67e5\u786e\u4fdd\u8c03\u7528\u6b63\u786e\u7684\u51fd\u6570\u7248\u672c / Type checking ensures the correct function version is called\r\n- \u6e05\u6670\u7684\u9519\u8bef\u63d0\u793a / Clear error messages\r\n- \u652f\u6301\u8be6\u7ec6\u7684\u7c7b\u578b\u6ce8\u89e3 / Supports detailed type annotations\r\n\r\n## \u5b89\u88c5 / Installation\r\n\r\n```bash\r\npip install overload_function_1\r\n```\r\n\r\n## \u4f7f\u7528\u793a\u4f8b / Usage Example\r\n\r\n```python\r\nfrom typing import Union\r\nfrom overload_function import OverloadFunction\r\n\r\n@OverloadFunction\r\ndef process_data(a: int, b: str) -> None:\r\n print(f\"Processing int {a} and string '{b}'\")\r\n\r\n@process_data.overload\r\ndef process_data(a: str, b: int) -> None:\r\n print(f\"Processing string '{a}' and int {b}\")\r\n\r\n@process_data.overload\r\ndef process_data(a: Union[int, float], b: Union[int, float]) -> None:\r\n print(f\"Processing numbers {a} and {b}\")\r\n\r\n# \u6b63\u786e\u8c03\u7528 / Correct calls\r\nprocess_data(1, \"hello\") # \u8c03\u7528\u7b2c\u4e00\u4e2a\u7248\u672c / Calls first version\r\nprocess_data(\"hello\", 2) # \u8c03\u7528\u7b2c\u4e8c\u4e2a\u7248\u672c / Calls second version\r\nprocess_data(3.14, 42) # \u8c03\u7528\u7b2c\u4e09\u4e2a\u7248\u672c / Calls third version\r\n\r\n# \u9519\u8bef\u8c03\u7528 / Incorrect calls\r\nprocess_data(\"hello\", \"world\") # \u629b\u51fa TypeError / Raises TypeError\r\nprocess_data([], {}) # \u629b\u51fa TypeError / Raises TypeError\r\n```\r\n\r\n## \u6ce8\u610f\u4e8b\u9879 / Notes\r\n\r\n1. **\u4e0d\u652f\u6301\u53ef\u53d8\u53c2\u6570** / **No support for variable arguments**:\r\n - \u4e0d\u80fd\u88c5\u9970\u5305\u542b `*args` \u6216 `**kwargs` \u7684\u51fd\u6570\r\n - Cannot decorate functions with `*args` or `**kwargs` parameters\r\n\r\n2. **\u7c7b\u578b\u6ce8\u89e3\u63a8\u8350** / **Type annotations recommended**:\r\n - \u4e3a\u83b7\u5f97\u6700\u4f73\u6548\u679c\uff0c\u8bf7\u4e3a\u51fd\u6570\u53c2\u6570\u6dfb\u52a0\u7c7b\u578b\u6ce8\u89e3\r\n - For best results, add type annotations to function parameters\r\n\r\n3. **\u6027\u80fd\u8003\u8651** / **Performance considerations**:\r\n - \u6bcf\u6b21\u8c03\u7528\u90fd\u4f1a\u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\uff0c\u53ef\u80fd\u5f71\u54cd\u6027\u80fd\r\n - Type checking on each call may impact performance\r\n\r\n4. **\u660e\u786e\u6027** / **Explicitness**:\r\n - \u5f53\u591a\u4e2a\u51fd\u6570\u7248\u672c\u5339\u914d\u65f6\uff0c\u4f1a\u629b\u51fa\u5f02\u5e38\r\n - Raises an exception when multiple function versions match\r\n\r\n## \u8bb8\u53ef\u8bc1 / License\r\n\r\nMIT License\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python decorator for function overloading based on argument types",
"version": "0.1.0",
"project_urls": {
"Bug Reports": "https://github.com/your-repo/overload-function/issues",
"Homepage": "https://github.com/Locked-chess-official/overload-function",
"Source": "https://github.com/your-repo/overload-function"
},
"split_keywords": [
"overload",
"function",
"decorator",
"type",
"checking"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "41568e2915517d316f6d35f0e90155259201b444fb2158ce5bd2a07dc81476b2",
"md5": "30958e2b6a259718b0c112ef3b6803d2",
"sha256": "8fc93fe303fd067cb5d02462c2aa740bc789da0657a89b1347bf4acb5cb1dab1"
},
"downloads": -1,
"filename": "overload_function_1-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "30958e2b6a259718b0c112ef3b6803d2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 4954,
"upload_time": "2025-07-20T14:49:12",
"upload_time_iso_8601": "2025-07-20T14:49:12.774657Z",
"url": "https://files.pythonhosted.org/packages/41/56/8e2915517d316f6d35f0e90155259201b444fb2158ce5bd2a07dc81476b2/overload_function_1-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "7f2574a994e8b1c3375d993b52df69c56f42b054a256b324f519b147e3248574",
"md5": "ec98f330c6424e1c20d7f6e691dbfd79",
"sha256": "9ea413cbc34bbd61f1adf25a8e6ebb7201b2de148658345915d3b05e505df5f2"
},
"downloads": -1,
"filename": "overload_function_1-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "ec98f330c6424e1c20d7f6e691dbfd79",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 4838,
"upload_time": "2025-07-20T14:49:14",
"upload_time_iso_8601": "2025-07-20T14:49:14.340030Z",
"url": "https://files.pythonhosted.org/packages/7f/25/74a994e8b1c3375d993b52df69c56f42b054a256b324f519b147e3248574/overload_function_1-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-20 14:49:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Locked-chess-official",
"github_project": "overload-function",
"github_not_found": true,
"lcname": "overload-function-1"
}