小编Bri*_*ian的帖子

类型注释的 None 与 NoneType

如果函数可以返回None,那么不应该使用类型注释吗NoneType

例如,我们不应该使用这个:

from types import NoneType

def my_function(num: int) -> int | NoneType:

    if num > 0:
        return num

    return None
Run Code Online (Sandbox Code Playgroud)

代替:

def my_function(num: int) -> int | None:

    if num > 0:
        return num

    return None
Run Code Online (Sandbox Code Playgroud)

python typing nonetype python-typing

6
推荐指数
1
解决办法
2088
查看次数

标签 统计

nonetype ×1

python ×1

python-typing ×1

typing ×1