gpt4 book ai didi

python - @overload 签名的访问列表

转载 作者:行者123 更新时间:2023-12-02 18:03:34 24 4
gpt4 key购买 nike

可以使用 inspect.signature 在 python 中获取函数的签名。

from typing import overload
import inspect

class Foo:
@overload
def foo(x: int, y: float): ...
@overload
def foo(y: float): ...
def foo(a, b=None):
if b is None:
x, y = 1, a
else:
x, y = a, b
return y / x

print(inspect.signature(Foo.foo)) # (a, b=None)

有什么方法可以同时查看此类函数的所有重载的签名吗?在这个例子中,我想得到一个列表 [inspect.Signature(x: int, y: float), inspect.Signature(y: float)] 或类似的东西。

最佳答案

Before Python 3.11 is released with this PR that adds typing.get_overloads() ,不是在运行时,因为 @overloadfoo 没有任何作用,它只是类型检查器理解的装饰器。

关于python - @overload 签名的访问列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73786376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com