没有 return 值的 Python 函数是否应该在其签名中包含 `-> None`?
Should a Python function that doesn't return a value have `-> None` in its signature?
在 Python 中编写一个没有 return 值的类型化函数(如其他语言中的“void”)时,最好 and/or 常规地标记它如下?
def say_hello(name: str) -> None
print(f"Hello, {name}.")
或者,是否应该省略 -> None
?
def say_hello(name: str)
我想我想问的是省略 -> None
是否会使 return 类型未指定,或者它被假定为 NoneType
,因此没有必要?
The default for a missing annotation is Any
, not None
. 如果您省略 return 注释,类型检查器会将您的函数视为 returning Any
.
For a checked function, the default annotation for arguments and for the return type is Any.
在 Python 中编写一个没有 return 值的类型化函数(如其他语言中的“void”)时,最好 and/or 常规地标记它如下?
def say_hello(name: str) -> None
print(f"Hello, {name}.")
或者,是否应该省略 -> None
?
def say_hello(name: str)
我想我想问的是省略 -> None
是否会使 return 类型未指定,或者它被假定为 NoneType
,因此没有必要?
The default for a missing annotation is Any
, not None
. 如果您省略 return 注释,类型检查器会将您的函数视为 returning Any
.
For a checked function, the default annotation for arguments and for the return type is Any.