Why does Python allow function calls with wrong number of arguments?

Solution 1:

Python cannot know up-front what object you'll end up calling, because being dynamic, you can swap out the function object. At any time. And each of these objects can have a different number of arguments.

Here is an extreme example:

import random

def foo(): pass
def bar(arg1): pass
def baz(arg1, arg2): pass

the_function = random.choice([foo, bar, baz])
print(the_function())

The above code has a 2 in 3 chance of raising an exception. But Python cannot know a-priori if that'll be the case or not!

And I haven't even started with dynamic module imports, dynamic function generation, other callable objects (any object with a __call__ method can be called), or catch-all arguments (*args and **kwargs).

But to make this extra clear, you state in your question:

It is not going to change while the program is running.

This is not the case, not in Python, once the module is loaded you can delete, add or replace any object in the module namespace, including function objects.

Solution 2:

The number of arguments being passed is known, but not the function which is actually called. See this example:

def foo():
    print("I take no arguments.")

def bar():
    print("I call foo")
    foo()

This might seem obvious, but let us put these into a file called "fubar.py". Now, in an interactive Python session, do this:

>>> import fubar
>>> fubar.foo()
I take no arguments.
>>> fubar.bar()
I call foo
I take no arguments.

That was obvious. Now for the fun part. We’ll define a function which requires a non-zero amount of arguments:

>>> def notfoo(a):
...    print("I take arguments!")
...

Now we do something which is called monkey patching. We can in fact replace the function foo in the fubar module:

>>> fubar.foo = notfoo

Now, when we call bar, a TypeError will be raised; the name foo now refers to the function we defined above instead of the original function formerly-known-as-foo.

>>> fubar.bar()
I call foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/horazont/tmp/fubar.py", line 6, in bar
    foo()
TypeError: notfoo() missing 1 required positional argument: 'a'

So even in a situation like this, where it might seem very obvious that the called function foo takes no arguments, Python can only know that it is actually the foo function which is being called when it executes that source line.

This is a property of Python which makes it powerful, but it also causes some of its slowness. In fact, making modules read-only to improve performance has been discussed on the python-ideas mailinglist some time ago, but it didn't gain any real support.