0

I am new to Python. When I use VS Code to type a __init__ function, it auto-supplies some text like below.

def __init__(self, ...) -> None:

I am not exactly sure what -> None means here after some searching.

wjandrea
  • 23,210
  • 7
  • 49
  • 68
  • In this case, `-> None` is a type hint for the return of the function. init is not allowed to return anything, so its return type is always `None`. It does not affect the behavior of your function. – NGilbert Dec 03 '21 at 21:22
  • It's a function annotation that provide a type hint (https://docs.python.org/3/library/typing.html) – defladamouse Dec 03 '21 at 21:22
  • 1
    @NGilbert To be a bit pedantic, `__init__` does return something, it's just that something should always be `None`. Functions that don't return anything are annotated with [`NoReturn`](https://docs.python.org/3/library/typing.html#typing.NoReturn), not with `None`. – Brian Dec 03 '21 at 21:26
  • 2
    In the future, [please](https://meta.stackoverflow.com/questions/261592) attempt at least some research before asking questions here. In this case, it's as simple as putting `python arrow after function definition` [into a search engine](https://duckduckgo.com/?q=python+arrow+after+function+definition). – Karl Knechtel Dec 03 '21 at 21:27
  • @Brian I appreciate the correction, thanks – NGilbert Dec 03 '21 at 21:29
  • @Brian To be clear, `NoReturn` is for functions that don't return at all. It's impossible for a Python function to return nothing. – wjandrea Dec 03 '21 at 21:42

0 Answers0