如何返回类型提示定义的类型



如果我有以下内容:

def alert(self, text: str, color: str) -> str:

我可以以某种方式获得我的参数的指定类型吗?

我尝试使用func.__code__.co_varnames&inspect.getargspec(func)没有良好的结果。

alert.__annotations__具有类型的dict。

密钥return描述了返回值。所有其他键是参数的名称。

blue_note的答案与python 3.7中引入的 __future__.annotations不起作用,使用typing.get_type_hints代替。

最新更新