我如何 return 键入提示定义的类型
How do I return type hinting defined types
如果我有以下情况:
def alert(self, text: str, color: str) -> str:
我能以某种方式获得指定类型的参数吗?
我尝试使用 func.__code__.co_varnames
和 inspect.getargspec(func)
,但效果不佳。
alert.__annotations__
有一个带有类型的字典。
键 return
描述了 return 值。所有其他键都是参数的名称。
blue_note 的答案不适用于 __future__.annotations
,在 python 3.7 中引入,请改用 typing.get_type_hints。
如果我有以下情况:
def alert(self, text: str, color: str) -> str:
我能以某种方式获得指定类型的参数吗?
我尝试使用 func.__code__.co_varnames
和 inspect.getargspec(func)
,但效果不佳。
alert.__annotations__
有一个带有类型的字典。
键 return
描述了 return 值。所有其他键都是参数的名称。
blue_note 的答案不适用于 __future__.annotations
,在 python 3.7 中引入,请改用 typing.get_type_hints。