在 Python 中键入注释
Type annotation in Python
如果参数可以有不同的类型,如何在 Python 中写 :type 注释?
"""
:type param_name: type1|type2
"""
或
"""
:type param_name: type1 / type2
"""
PyCharm 接受第二个变体
您正在使用 Sphinx 项目表示法,顺便说一下,它被拒绝包含在 PEP 484 -- Type Hints proposal 中。
:type
是一个 info field list,实际上并没有那么多关于这些的正式规范。文档示例使用 or
:
:type priority: integer or None
但请注意 integer
不是正式类型,None
也不是(它是单例对象)。
这些是 文档 结构,不是类型提示,真的。 PyCharm 完全支持这些很好,但这些不是 Python 标准。
我会坚持使用正确的类型注释。这意味着使用 Union
type:
Union[type1, type2]
如果需要支持Python 2.
,可以将这些放在# type:
评论中
如果参数可以有不同的类型,如何在 Python 中写 :type 注释?
"""
:type param_name: type1|type2
"""
或
"""
:type param_name: type1 / type2
"""
PyCharm 接受第二个变体
您正在使用 Sphinx 项目表示法,顺便说一下,它被拒绝包含在 PEP 484 -- Type Hints proposal 中。
:type
是一个 info field list,实际上并没有那么多关于这些的正式规范。文档示例使用 or
:
:type priority: integer or None
但请注意 integer
不是正式类型,None
也不是(它是单例对象)。
这些是 文档 结构,不是类型提示,真的。 PyCharm 完全支持这些很好,但这些不是 Python 标准。
我会坚持使用正确的类型注释。这意味着使用 Union
type:
Union[type1, type2]
如果需要支持Python 2.
,可以将这些放在# type:
评论中