【发布时间】:2020-07-03 23:27:28
【问题描述】:
我想获取对象属性的类型提示。我只能得到类的提示,而不是它的实例。
我曾尝试使用here 中的foo_instance.__class__,但这仅显示类变量。
那么在示例中如何获得bar 的类型提示?
class foo:
var: int = 42
def __init__(self):
self.bar: int = 2
print(get_type_hints(foo)) # returns {'var': <class 'int'>}
【问题讨论】:
标签: python python-3.x type-hinting