Python 3.6支持类型注释,例如:
def foo() -> int:
return 42
如果一个函数没有返回任何内容,应该使用什么?PEP484 的示例通常将 None
用作返回类型,但也可以使用 typing
包中的 NoReturn
类型。
因此,问题是什么是首选使用的,以及什么被认为是最佳实践:
def foo() -> None:
#do smth
或者from typing import NoReturn
def foo() -> NoReturn:
#do smth
None
的类型不是None
,而是NoneType
。 - Willem Van OnsemNone
在类型提示中被视为等同于type(None)
,即NoneType
。 - ByAgenT