Correct Type annotation for __init__

久未见 提交于 2021-01-20 18:49:10

问题


What is the correct type annotation for a __init__ function in python?

class MyClass:
    ...

Which of the following would make more sense?

def __init__(self):
    # type: (None) -> None

def __init__(self):
    # type: (MyClass) -> MyClass

def __init__(self):
    # type: (None) -> MyClass

Since we would normally instantiate as myclass = MyClass(), but the __init__ function itself has no return value.


回答1:


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.



来源:https://stackoverflow.com/questions/46779046/correct-type-annotation-for-init

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!