卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章79808本站已运行4416

Python 中的重载函数

python 中的重载函数

函数重载是指定义多个具有相同名称但不同签名的函数的能力,这意味着它们具有不同数量或类型的参数。编译器或解释器会根据函数调用期间传递的参数数量和类型自动选择函数的正确版本。

java 和 c++++ 等语言本身就支持此功能。

虽然 python 本身不支持函数重载,因为它是一种动态类型语言,但可以使用各种模块和实用程序来实现相同的功能。

这是我的重载实现。

立即学习“Python免费学习笔记(深入)”;

执行

from __future__ import annotations

import inspect
import typing

bin: dict[str, overloadnamespace] = {}


class overloadnamespace:
    overloads: dict[tuple[type, ...], typing.callable[..., typing.any]]
    fallback: typing.callable[..., typing.any]

    def __init__(self, name: str) -> none:
        self.overloads = {}
        self.fallback = self._fallback
        bin[name] = self

    def __call__(self, *args: typing.any, **kwds: typing.any) -> typing.any:
        types = [type(arg) for arg in args]
        types.extend([type(kwrg) for kwrg in kwds])
        try:
            return self.overloads[tuple(types)](*args, **kwds)
        except keyerror:
            return self.fallback(*args, **kwds)

    @staticmethod
    def _fallback(*_, **__) -> none:
        raise notimplementederror

overloadnamespace 类是一个可调用类,充当函数名称和调用签名之间的媒介。参数被传递到 __call__ dunder 方法中,该方法将提供的数据类型与存储在重载字典中的类型元组进行匹配。返回匹配的签名,并使用提供的 args/kwargs 进行调用。如果没有找到匹配的签名,则调用后备函数。

使用 overloadnamespace 类

此类不适合手动使用,它由装饰器使用,修饰器修改函数并使用与函数提供的名称相同的名称返回 overloadnamespace 类的实例。

def overload(*args) -> typing.callable[..., overloadnamespace] | overloadnamespace:
    """decorator used to create overloads of functions with same name. returns a [overloadnamespace]"""
    if len(args) == 1 and inspect.isfunction(args[0]):
        return overload_using_types(args[0])

    def inner(func: typing.callable[..., typing.any]) -> overloadnamespace:
        sig = inspect.signature(func)
        assert len(args) == len(
            sig.parameters
        ), "number of types and args in function is not same."

        namespace = (
            bin[func.__name__]
            if bin.get(func.__name__)
            else overloadnamespace(func.__name__)
        )
        namespace.overloads[tuple(args)] = func
        return namespace

    return inner

def overload_using_types(func: typing.callable[..., typing.any]) -> overloadnamespace:
    args = inspect.signature(func).parameters
    types = tuple(arg.annotation for arg in args.values())

    namespace = (
        bin[func.__name__]
        if bin.get(func.__name__)
        else overloadnamespace(func.__name__)
    )

    namespace.overloads[types] = func
    return namespace

重载装饰器使用装饰器值或类型提示检查参数类型并返回命名空间类。

使用示例

# types in decorator
@overload(int, int)
def sum(a, b):
    return a+b

# or as typehints
@overload 
def sum(a: float, b: float):
    return int(a+b)+1

sum(1,2) # 3
sum(1.23, 2.0) # 4

这只是一个基本想法,适用于非联合固定类型。

倒退

后备函数用作当没有参数模式与调用模式匹配时要调用的函数。

def fallback(
    func: typing.Callable[..., typing.Any],
) -> OverloadNamespace:
    """Fallback function to be called if no overloads match to the provided arguments."""
    namespace = (
        bin[func.__name__]
        if bin.get(func.__name__)
        else OverloadNamespace(func.__name__)
    )
    namespace.fallback = func
    return namespace

@fallback 
def sum(*args):
    return sum(args)

sum(1,2,3,4) # 10
卓越飞翔博客
上一篇: 克隆 Netflix 以提升您的前端技能
下一篇: Golang 函数的性能提升秘诀:极致优化解析
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏