python - __init__ 的正确类型注释

标签 python python-2.7 typing

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/

相关文章:

Python:根据对象属性从列表中删除对象

python - AWS Lambda 读取作为源代码上传的 zip 文件的内容

algorithm - 我的数组等边算法出现意外结果

python-2.7 - 从保存的电子邮件中提取 Outlook 附件

function - 运算符的 Haskell 类型优先级高于函数

python - 由于无法选择表 id 属性,如何使用 BeautifulSoup 抓取表?

python - 在 python 上执行类似单个链接的操作

python - 将命名元组的值从字符串转换为整数

iframe - 使用 Selenium IDE 输入 IFrame

lua - Lua 中记录类型的约定是什么?