简体   繁体   English

为什么 Visual Studio Code 在派生类中插入“return super().__init__(self)”?

[英]Why does Visual Studio Code insert "return super().__init__(self)" into the derived class?

I understand that __init__() is required to return None , but when Visual Studio autocompletes a derived class __init__() for me, it does so like this:我知道__init__()需要返回None ,但是当 Visual Studio 为我自动完成派生类__init__() ,它__init__()

class Base:
    def __init__(self):
        print('Base')

class Derived(Base):
    def __init__(self):
        return super().__init__()  # This part is added by VS Code

It's obviously not a syntax issue, as the Base __init__ is returning None , which the derived class in turn returns as well.这显然不是语法问题,因为 Base __init__返回None ,派生类反过来也返回None

But why even bother having that?但为什么还要麻烦呢? What purpose does the return statement serve here? return 语句在这里有什么作用?

As per @Simeon Visser's comment , VS Code seems to do this for all inherited methods, and does not discriminate between __init__() and other methods.根据@Simeon Visser 的评论,VS Code 似乎对所有继承的方法都这样做,并且不区分__init__()和其他方法。 An issue hasalready been filed in the Python Language Server repo. Python 语言服务器存储库中已经提交了一个问题。 The issue has been fixed .该问题已修复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined - why does super(MainWindow, self),__init__(*args, **kwargs) NameError: name '__init__' is not defined 为什么不在 Pytorch 中使用 super().__init__(Model,self) - Why not super().__init__(Model,self) in Pytorch 为什么在父类__init __()中调用super()会改变子类__init __()的行为? - Why does calling super() in parent class __init__() change the subclass __init__() behavior? 为什么使用 __init__(self, **kwargs) 会返回以下错误代码:SyntaxError: invalid syntax? - Why does using __init__(self, **kwargs) return the following error code: SyntaxError: invalid syntax? 为什么在多次继承中执行Base .__ init __(self)而不是super().__ init __()时会跳过__init__? - Why is an __init__ skipped when doing Base.__init__(self) in multiple inheritance instead of super().__init__()? 为什么 super()() 和 super().__init__() 不一样? - Why does super()() not do the same as super().__init__()? 为什么super(A,self).__ init __()不调用A的__init __()? - Why super(A, self).__init__() doesn't call A's __init__()? Python超类__init __()? - Python super class __init__()? 为什么允许在 Python 中使用 super 从 __init__ 返回一个值? - Why allow to return a value from __init__ with super in Python? Python:super和__init __()vs __init __(self) - Python: super and __init__() vs __init__( self )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM