Skip to main content

列表

官方列表

魔术属性

名称作用
__name__当前的入口文件名,常用来测试
__doc__
__qualname__
__module__
__code__
__globals__
__dict__
__closure__
__annotations__所有注解组成的一个字段官方建议通过inspect.get_annotations()来获取注解字典
__kwdefaults__

__annotations__

Python 3.10 在标准库中加入了一个新函数:inspect.get_annotations()。在 Python 3.10 以上的版本中,调用该函数就是访问对象注解字典的最佳做法。该函数还可以“解析”字符串形式的注解。

import inspect
def test(pa1: int) -> int:
return pa1

# 3.9之前

print(test.__annotations__)
# 两者等价
print(inspect.get_annotations(test))