Python void 返回类型注解

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36797282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:21:49  来源:igfitidea点击:

Python void return type annotation

pythonannotationsvoidtype-hinting

提问by Tregoreg

In python 3.x, it is common to use return type annotation of a function, such as:

在python 3.x中,通常使用函数的返回类型注解,例如:

def foo() -> str:
    return "bar"

What is the correct annotation for the "void" type?

“void”类型的正确注释是什么?

I'm considering 3 options:

我正在考虑 3 个选项:

  1. def foo() -> None:
    • not logical IMO, because Noneis not a type,
  2. def foo() -> type(None):
    • using the best syntax I know for obtaining NoneType,
  3. def foo():
    • omit explicit return type information.
  1. def foo() -> None:
    • 不符合逻辑的 IMO,因为None它不是一种类型,
  2. def foo() -> type(None):
    • 使用我所知道的最佳语法来获取NoneType
  3. def foo():
    • 省略显式返回类型信息。

Option 2. seems the most logical to me, but I've already seen some instances of 1.

选项 2. 对我来说似乎最合乎逻辑,但我已经看到了 1 的一些实例。

回答by AKS

This is straight from PEP 484 -- Type Hintsdocumentation:

这直接来自PEP 484 -- Type Hints文档:

When used in a type hint, the expression Noneis considered equivalent to type(None).

在类型提示中使用时,该表达式None被视为等效于type(None).

And, as you can see most of the examples use Noneas return type.

而且,正如您所看到的,大多数示例都None用作返回类型。

回答by MisterMiyagi

TLDR: The idiomatic equivalent of a voidreturn type annotation is -> None.

TLDR:void返回类型注释的惯用等价物是-> None.

def foo() -> None:
    pass


Type hinting in Python does not strictly require actual types. For example, annotations may use strings of type names: Union[str, int], Union[str, 'int'], 'Union[str, int]'and various variants are equivalent.

Python 中的类型提示并不严格要求实际类型。例如,注释可以使用类型名称的字符串:Union[str, int]Union[str, 'int']'Union[str, int]'和各种变型是等价的。

Similarly, the type annotation Noneis considered to mean"is of NoneType". This can be used not just for return types, though you will see it most often there:

类似地,类型注释None被认为意味着“是NoneType”。这不仅可以用于返回类型,但您最常在那里看到它:

bar : None

def foo(baz: None) -> None:
    return None

This also applies to generic types. For example, you can use Nonein Generator[int, None, None]to indicate a generator does not take or return values.

这也适用于泛型类型。例如,您可以使用NoneinGenerator[int, None, None]来指示生成器不接受或不返回值。



Even though PEP 484 suggests that Nonemeans type(None), you should notuse the latter form explicitly. The type hinting specification does notinclude any form of type(...). This is technically a runtime expression, and its support is entirely up to the type checker. The mypyproject is considering to remove supportfor type(None)and remove it from 484 as well.

即使 PEP 484 建议这None意味着type(None),您也不应该明确使用后一种形式。该类型提示规范并没有包括任何形式的type(...)。这在技术上是一个运行时表达式,它的支持完全取决于类型检查器。该mypy项目正在考虑取消type(None)484 的支持并将其从 484 中移除。

Or maybe we should update PEP 484 to not suggest that type(None)is valid as a type, and Noneis the only correct spelling? There should one -- and preferably only one -- obvious way to do it etc.

--- JukkaL, 18 May 2018

或者也许我们应该更新 PEP 484 以不建议它type(None)作为一种有效的类型,并且None是唯一正确的拼写?应该有一种 - 最好只有一种 - 明显的方法等等。

--- JukkaL,2018 年 5 月 18 日



Omitting the return type does notmean that there is no return value. As per PEP 484:

省略返回类型也并不意味着就没有返回值。根据PEP 484

For a checked function, the default annotation for arguments and for the return type is Any.

对于已检查的函数,参数和返回类型的默认注释是Any.

This means the value is considered dynamically typed and statically supports any operation. That is practically the opposite meaning of void.

这意味着该值被视为动态类型并静态支持任何操作。这实际上是 的相反含义void