【发布时间】:2018-06-10 19:29:28
【问题描述】:
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 Onsem,根据 PEP484,
None在类型提示中被认为等同于type(None)什么是NoneType。
标签: python annotations python-3.6 type-hinting typing