Search code examples
pythoninheritancesuper

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:

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.

But why even bother having that? What purpose does the return statement serve here?


Solution

  • 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. An issue has already been filed in the Python Language Server repo. The issue has been fixed.