typehints>无或留白

2024-04-29 12:52:00 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python3,可以选择使用typehints。在

我的问题是,如果一个函数返回None,应该添加它,还是留空。在

def hint(p:str) -> None:
    pass

def no_hint(p:str):
    pass

哪个政治公众人物解决了这个问题?在


Tags: 函数nononedefpass政治python3公众
1条回答
网友
1楼 · 发布于 2024-04-29 12:52:00

显式并且对于返回None的函数,始终包含-> None

这是因为,对于不带参数的函数,类型检查器将假定您根本没有使用类型提示。例如,def foo():是要返回None,还是只是没有提示类型?在

PEP 484 - Type Hints间接地解决了这个问题:

Note that the return type of __init__ ought to be annotated with -> None. The reason for this is subtle. If __init__ assumed a return annotation of -> None, would that mean that an argument-less, un-annotated __init__ method should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that __init__ ought to have a return annotation; the default behavior is thus the same as for other methods.

相关问题 更多 >