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.
显式并且对于返回
None
的函数,始终包含-> None
这是因为,对于不带参数的函数,类型检查器将假定您根本没有使用类型提示。例如,
def foo():
是要返回None
,还是只是没有提示类型?在PEP 484 - Type Hints间接地解决了这个问题:
相关问题 更多 >
编程相关推荐