I'm trying to understand what are the benefits of the fact that python defaults to not checking function arguments.
For example:
from some_module import my_function
my_function(some_argument)
# some_argument could be a very complicated data structure
# made of dictionaries and lists.
# I have to depend on the good will of the developer of my_function
# to get reasonable error logs/exceptions.
# If the type checks were completed for function arguments on the level of the language
# it would be much clearer that the function is failing because of the wrong structure of the argument
The above is an attempt to reason why it would be a positive if type checking for arguments existed as a core functionality of the language.
What are the positive sides of the current status quo (no argument type checks)?
1 Answer 1
Simpler, cheaper language implementation.
Faster compilation or execution (depending on when the checking would run).
Often better readability of simple algorithms.
Easier to write flexible functions; e.g. functions that can take scalar and compound arguments (i.e. the aggregate pattern can be used without having to define classes only for that purpose).
Trade-off between the price and benefit of compile/runtime type checking can be made by the user (after all, the
typing
module exists) rather by having the language making it for him.
There are downsides of course, but it looks as if you already know them.
-
Completely missed that
Simpler, cheaper language implementation
might be a reason :DTheMeaningfulEngineer– TheMeaningfulEngineer2020年08月14日 09:06:57 +00:00Commented Aug 14, 2020 at 9:06 -
Almost certainly not faster execution. Type systems enabled better optimizations, in two ways: 1) they give the compiler more type information 2) they impose restrictions on code. #1 gives the compiler more information to work with (which a JIT might need to figure out for itself, e.g. this function is only ever called with an
Int
, never aDouble
). #2 restricts the possible observable behaviors of a piece of code. The less edge cases there, the more room an optimizer has to change things without violating the "as if" rule.Alexander– Alexander2020年10月06日 15:57:37 +00:00Commented Oct 6, 2020 at 15:57
typing
module in version 3.5. Python itself ignores these type annotations, but you can use the externalmypy
tool as a type checker.