如果我这样做:
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
的类型提示?
最佳答案
我最终写了一些东西,使用 inspect
、ast
、exec
和 eval
来解决这个问题我。
https://gist.github.com/sweettuse/f5fa022a0abadf3fc368105fcccc46c1
关于python - 成员变量的注解在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48997386/