[英]Retrieve typing.List[X] type
对于以下类型:
import typing
foo: typing.List[int] = []
如何检索 foo 列表中的项目类型(通过类型提示)? 结果必须是<class 'int'>
。
不确定您是否正在寻找这样的东西。
import sys
import typing
foo: typing.List[int] = []
foo_one: typing.Tuple[float] = ()
def _get_type_of_item(param):
"""
typing.get_type_hints(obj[, globals[, locals]]):
Return a dictionary containing type hints for a function, method, module or class object.
"""
return typing.get_type_hints(sys.modules[__name__])[param].__args__
print(_get_type_of_item('foo')) # this will print (<class 'int'>,)
print(_get_type_of_item('foo_one')) # this will print (<class 'float'>,)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.