Just in case that’s a genuine question, the reasons people like static types are:
- Catch more bugs.
- Catches bugs earlier (while you are writing the code). This is sometimes called “shift left”.
- Fewer tests needed.
- Code is easier to understand.
- Code is easier to navigate.
- Refactoring is much easier.
- Development speed is faster (due to the above points).
Often people say it slows development down but it’s actually the opposite. Especially for large projects or ones involving multiple people.
The only downside really is that sometimes the types can get more complicated than they’re worth, but in that case you have an escape hatch via the Any
type.
No. You’ll have to be more specific about what kind of polymorphism you mean (it’s an overloaded term), but you can have type unions, like
int | str
.Not unless you have ridiculously exhaustive tests, which you definitely don’t. And running tests is still slower than your editor telling you of your mistake immediately.
I probably didn’t explain 4-6 well enough if you haven’t actually ever used static types.
They make it easier to navigate because your IDE now understands your code and you can do things like “find all references”, and “go to definition”. With static types you can e.g. ctrl-click on
mystruct.myfield
and it will go straight to the definition ofmyfield
.They make the code easier to understand because knowing the types of variables tells you a lot of information about what they are and how to use them. You’ll often see in untyped code people add comments saying what type things are anyway.
Refactoring is easier because your IDE understands your code, so you can do things like renaming variables and moving code and it will update all the things it needs to correctly. Refactoring is also one of those areas where it tends to catch a lot of mistakes. E.g. if you change the type of something or the parameters of a function, it’s very easily to miss one place where it was used.
I don’t think “you need to learn it” really counts as slowing down development. It’s not that hard anyway.
It’s probably best not to use Python for anything, but here we are.
I will grant that data science is probably one of the very few areas where you may not want to bother, since I would imagine most of your code is run exactly once. So that might explain why you don’t see it as worthwhile. For code that is long-lived it is very very obviously worth it.