Skip to main content

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

  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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

overload_function_1-0.1.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

overload_function_1-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

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

Hashes for overload_function_1-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9ea413cbc34bbd61f1adf25a8e6ebb7201b2de148658345915d3b05e505df5f2
MD5 ec98f330c6424e1c20d7f6e691dbfd79
BLAKE2b-256 7f2574a994e8b1c3375d993b52df69c56f42b054a256b324f519b147e3248574

See more details on using hashes here.

File details

Details for the file overload_function_1-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for overload_function_1-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fc93fe303fd067cb5d02462c2aa740bc789da0657a89b1347bf4acb5cb1dab1
MD5 30958e2b6a259718b0c112ef3b6803d2
BLAKE2b-256 41568e2915517d316f6d35f0e90155259201b444fb2158ce5bd2a07dc81476b2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page