typehints -> None 或留空
typehints -> None or leave blank
使用python 3,可以选择使用类型提示。
我的问题是,如果一个函数 returns None,应该添加这个,还是留空。
即
def hint(p:str) -> None:
pass
def no_hint(p:str):
pass
哪个 PEP 解决了这个问题?
显式 并始终包含 -> None
用于 return None
的函数
这是因为,否则,对于不带参数的函数,类型检查器将假设您根本没有使用类型提示。例如,def foo():
会变成 return 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.
使用python 3,可以选择使用类型提示。
我的问题是,如果一个函数 returns None,应该添加这个,还是留空。
即
def hint(p:str) -> None:
pass
def no_hint(p:str):
pass
哪个 PEP 解决了这个问题?
显式 并始终包含 -> None
用于 return None
这是因为,否则,对于不带参数的函数,类型检查器将假设您根本没有使用类型提示。例如,def foo():
会变成 return 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.