python - 成员变量的注解在哪里

标签 python annotations python-3.6 abstract-syntax-tree inspect

如果我这样做:

def f(x: str):
    pass

如果我这样做,我可以很容易地看到 x 的这种类型提示:

print(f.__annotations__)

但是,如果我这样做,我似乎无法弄清楚注释的位置:

class C:
    def __init__(self):
        self.where_is_this_vars_annotation_stored: int = 4

有谁知道哪个 __annotations__ 字典包含 where_is_this_vars_annotation_stored 的类型提示?

最佳答案

我最终写了一些东西,使用 inspectastexeceval 来解决这个问题我。

https://gist.github.com/sweettuse/f5fa022a0abadf3fc368105fcccc46c1

关于python - 成员变量的注解在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48997386/

相关文章:

python - 请解释这个 python 循环,它计算为一个数组并被求和()

python - 如何在Python中使用群补?

java - 抽象枚举一个注解属性类型

c++ - eclipse CDT : Change Annotation icons on that left bar D: ?

java - 如何在单个注解中组合多个 Spring 测试注解?

python - 比较python中的2个字典并创建一个包含字典值的列表

python - 有没有办法在大括号内分解 f 弦?

python - Python flask-ask 中的 Amazon Alexa 主动事件请求

python - 在 sphinx 自动分类输出中排除 __init__ 参数

python 3.6 和 ValueError : loop argument must agree with Future