小编ell*_*493的帖子

如何获取对象属性的类型提示?

我想获取对象属性的类型提示。我只能获得该类的提示,而不能获得该类的实例。

我尝试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'>}
Run Code Online (Sandbox Code Playgroud)

python type-hinting python-3.x

11
推荐指数
2
解决办法
9175
查看次数

标签 统计

python ×1

python-3.x ×1

type-hinting ×1