When writing a typed function in Python that does not return a value (like "void" in other languages), is it best and/or conventional to mark it as follows?
def say_hello(name: str) -> None
print(f"Hello, {name}.")
Alternatively, should one just omit the -> None?
def say_hello(name: str)
I suppose what I'm asking is whether omitting the -> None leaves the return type unspecified, or it is assumed to be NoneType anyway, and thus unnecessary?
The default for a missing annotation is Any, not None. If you leave out the return annotation, type checkers will treat your function as returning Any.
For a checked function, the default annotation for arguments and for the return type is Any.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With