假设我定义一个类,其中包含带有类型提示的类级别变量(例如新的 python 3.7dataclasses
)
class Person:
name: str
age: int
def parse_me(self):
"what do I do here??"
我怎样才能得到这对(variable name, variable type)
?
typing.get_type_hints https://docs.python.org/3/library/typing.html#typing.get_type_hints是另一种不涉及直接访问魔法属性的方法:
from typing import get_type_hints
class Person:
name: str
age: int
get_type_hints(Person)
# returns {'name': <class 'str'>, 'age': <class 'int'>}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)