1

Visual Studio Code automatically generates the following when I create an __init__ method:

def __init__(self) -> None:
    pass

What's the significance of -> None and pass in the __init__ code?

The code works normally even when the -> None part is not added.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 2
    This may help > [link](https://stackoverflow.com/questions/64933298/why-should-we-use-in-def-init-self-n-none) – Nesi Jul 03 '22 at 10:44
  • 1
    As to "*Why does VS Code...*", I think you have an extension that does this for you, because _my_ VS Code doesn't do this. Check your installed Python-related extensions, some of it maybe auto-generating/auto-completing snippets of code. – Gino Mempin Jul 03 '22 at 12:01
  • The situation you mentioned does not appear on my machine, can you provide a list of extensions you have installed? This may be a function of an extension you have installed. – JialeDu Jul 04 '22 at 01:14

1 Answers1

1

Presumably it wants to demonstrate best current practice, and provide a completion which works without syntax errors.

Python doesn't require type annotations, but they are encouraged as best current practice. If you don't want to use type annotations, it's easy to remove the -> None.

Not including the type annotation in the autocompleted function definition would obviously also work, but leave more manual work and cognitive overhead for users who want to use type annotations.

Without the pass, saving and attempting to run the code would produce a syntax error. You are obviously expected to replace it with useful code, just like you will often want to add additional parameters inside the parentheses, etc.

Ultimately, if you dislike autocompletion, you can customize it or turn it off.

tripleee
  • 175,061
  • 34
  • 275
  • 318