3 ms·
Python duck typing can be as strong as you want it to be. You expect an int? Call int() on your parameter. Problem solved. If it's an int, great, you are GTG
by dbcurtis 9y ago
Python duck typing can be as strong as you want it to be. You expect an int? Call int() on your parameter. Problem solved. If it's an int, great, you are GTG. Was it a string containing digits? Great, it converts. Was it a user defined class that defines __int__()? Still GTG. Something that can't __int__()? It raises value error. Of course this only happens at run time. But I would argue that if you depend on compile-time type checking you are not testing everything you should be. unittest, hypothesis, and coverage are your friends.
Learn to write constructors well, and typing takes care of itself.
Explicit, make-it-this-gosh-darn-it typing is for performance acceleration: numpy, cython. Otherwise you should be using constructors that raise the appropriate ValueError exceptions. Embrace the duck. Otherwise, if you want to write Java, just go use Java to start with.