python 中 __init__
函数的正确类型注解是什么?
class MyClass:
...
以下哪项更有意义?
def __init__(self):
# type: (None) -> None
def __init__(self):
# type: (MyClass) -> MyClass
def __init__(self):
# type: (None) -> MyClass
因为我们通常会实例化为 myclass = MyClass()
,但是 __init__
函数本身没有返回值。
最佳答案
self
作为注释给出时应该从注解中省略,并且 __init__()
应该标记为 -> None
. PEP-0484 中明确指定了所有这些.
关于python - __init__ 的正确类型注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46779046/