__init__ 的正确类型注释 [英] Correct Type annotation for __init__
本文介绍了__init__ 的正确类型注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
python 中 __init__
函数的正确类型注释是什么?
What is the correct type annotation for a __init__
function in python?
class MyClass:
...
以下哪个更有意义?
def __init__(self):
# type: (None) -> None
def __init__(self):
# type: (MyClass) -> MyClass
def __init__(self):
# type: (None) -> MyClass
因为我们通常会实例化为 myclass = MyClass()
,但是 __init__
函数本身没有返回值.
Since we would normally instantiate as myclass = MyClass()
, but the __init__
function itself has no return value.
推荐答案
self
作为注释给出时,注释中应省略,__init__()
应标记为 ->无
.这在 PEP-0484 中有明确规定.
self
should be omitted from the annotation when it is given as a comment, and __init__()
should be marked as -> None
. This is all specified explicitly in PEP-0484.
这篇关于__init__ 的正确类型注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文