如何获取类变量和类型提示? [英] How to get class variables and type hints?
本文介绍了如何获取类变量和类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我定义了一个带有类型提示的类级别变量的类(例如类似于新的 python 3.7 dataclasses
)
Assume I define a class with class level variables with type hints (e.g. something like the new python 3.7 dataclasses
)
class Person:
name: str
age: int
def parse_me(self):
"what do I do here??"
如何获取(变量名,变量类型)
对?
推荐答案
typing.get_type_hints
是另一种不涉及直接访问魔法属性的方法:
typing.get_type_hints
is another method that doesn't involve accessing magic properties directly:
from typing import get_type_hints
class Person:
name: str
age: int
get_type_hints(Person)
# returns {'name': <class 'str'>, 'age': <class 'int'>}
这篇关于如何获取类变量和类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文