Is this a good practice to write -> None
when a function does not return anything ?
def nothing() -> None:
print("Hey I'm not returning anything!")
Yes, I would make a habit of this. If you're using type hints anyways, -> None
is more consistent than leaving it blank.
If you change what the function returns as well, the type hint on the return will also help narrow down the cause of type warnings.
Previously, I had written:
With a good IDE, you will also get warnings if you try to use the return value of
nothing
:# Warning: "Function 'nothing' doesn't return anything" (Pycharm) a = nothing()
Surprise surprise though, Pycharm is actually smart enough to infer that anyway even without the annotation.
With a dumber IDE, it may still make a difference in warnings produced, although I would expect that any IDE that's smart enough to make use of -> None
would also be smart enough to statically infer -> None
.
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