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

TheMeaningfulEngineer
  • 951
  • 2
  • 10
  • 17
  • 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 07:52
  • 5
    Does this answer your question? [Is there a real advantage to dynamic languages?](https://softwareengineering.stackexchange.com/questions/246762/is-there-a-real-advantage-to-dynamic-languages) – jonrsharpe Aug 14 '20 at 07:53

1 Answers1

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.

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310
  • Completely missed that `Simpler, cheaper language implementation` might be a reason :D – TheMeaningfulEngineer Aug 14 '20 at 09: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 06 '20 at 15:57