对于给定的数据类,如何获取有关字段类型的信息?在
示例:
>>> from dataclasses import dataclass, fields
>>> import typing
>>> @dataclass
... class Foo:
... bar: typing.List[int]
我可以向销售代表提供字段信息:
^{pr2}$我可以有一个我的酒吧领域的类型代表
>>> fields(Foo)[0].type
typing.List[int]
如何检索(作为python对象,而不是字符串repr):
typing.List
)typing.List
(int
)中项目的类型是吗?在
数据类字段的
type
属性它不是字符串表示,而是一种类型。在Python 3.6:
Python 3.7:
^{pr2}$在这种情况下,您可以使用
__args__
属性检索内部类型:相关问题 更多 >
编程相关推荐