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 个选项:
I'm considering 3 options:
def foo() ->无:
- 不是逻辑 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)
.
When used in a type hint, the expression
None
is considered equivalent totype(None)
.
而且,正如您所看到的,大多数示例都使用 None
作为返回类型.
And, as you can see most of the examples use None
as return type.
这篇关于Python void 返回类型注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文