我应该在 Python 3.9+ 中使用大写 L 列表进行类型提示吗?

Pre*_*sch 11 python python-typing

在 Python 3.9+ 中,我可以编写list_of_integers: list[int],但我看到高级开发人员使用旧语法(即使在 Python 3.9 和 3.10 脚本中):

from typing import List

list_of_integers: List[int]
Run Code Online (Sandbox Code Playgroud)

这对于向后兼容性和明确性来说是否优越?

Sor*_*ary 7

当当前版本的文档typing.List

自 3.9 版起已弃用builtins.list现在支持[]. 请参阅 PEP 585 和通用别名类型。

它应该被视为最佳实践,除非您有令人信服的理由不使用它(就像您所说的关于旧版本 Python 的向后兼容性)。