【发布时间】:2017-08-23 02:52:19
【问题描述】:
我想获取函数或类方法的注解。如果源代码中写了类型提示,那么我可以通过获取属性__annotations__来获取类型。
def hoge(n: int): ...
print(hoge.__annotations__) # {'n': <class 'int'>}
但我不知道如何获取写入存根文件 (.pyi) 中的类型。
# .pyi
def fuga(n: int): ...
# .py
def fuga(n): ...
print(fuga.__annotations__) # {}
有什么很酷的方法吗?
【问题讨论】:
标签: python python-3.x type-hinting