0

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)?

2
  • Type systems are complicated, so many "dynamic" languages like Perl, Python, PHP, Ruby, JavaScript don't have static type checks. This only changed fairly recently, e.g. Python got the typing module in version 3.5. Python itself ignores these type annotations, but you can use the external mypy tool as a type checker.
    – amon
    Aug 14 '20 at 7:52
  • 5
    Does this answer your question? Is there a real advantage to dynamic languages?
    – jonrsharpe
    Aug 14 '20 at 7:53
2
  • 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.

2
  • Completely missed that Simpler, cheaper language implementation might be a reason :D Aug 14 '20 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 a Double). #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
    Oct 6 '20 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.