Python void返回类型注释 [英] Python void return type annotation
本文介绍了Python void返回类型注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在python 3.x中,通常使用函数的返回类型注释,例如:
In python 3.x, it is common to use return type annotation of a function, such as:
def foo() -> str:
return "bar"
"void"类型的正确注释是什么?
What is the correct annotation for the "void" type?
我正在考虑3种选择:
-
def foo() -> None:
- 不是逻辑IMO,因为
None
不是类型,
- 不是逻辑IMO,因为
def foo() -> None:
- not logical IMO, because
None
is not a type,
- not logical IMO, because
- 使用我了解的最佳语法来获取
NoneType
,
- 省略显式的返回类型信息.
对我来说,选项2似乎是最合乎逻辑的,但是我已经看到过一些实例1.
Option 2. seems the most logical to me, but I've already seen some instances of 1.
推荐答案
这直接来自 PEP 484-类型提示文档:
在类型提示中使用时,表达式
None
被认为等效于type(None)
.
而且,如您所见,大多数示例都使用None
作为返回类型.
And, as you can see most of the examples use None
as return type.
这篇关于Python void返回类型注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文