What is a maximum number of arguments in a Python function?

In Python 3.7 and newer, there is no limit. This is the result of work done in issue #27213 and issue #12844; #27213 reworked the CALL_FUNCTION* family of opcodes for performance and simplicity (part of 3.6), freeing up the opcode argument to only encode a single argument count, and #12844 removed the compile-time check that prevented code with more arguments from being compiled.

So as of 3.7, with the EXTENDED_ARG() opcode, there is now no limit at all on how many arguments you can pass in using explicit arguments, save how many can be fitted onto the stack (so bound now by your memory):

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=7, micro=0, releaselevel="alpha", serial=2)
>>> def f(*args, **kwargs): pass
...
>>> exec("f({})".format(', '.join(map(str, range(256)))))
>>> exec("f({})".format(', '.join(map(str, range(2 ** 16)))))

Do note that lists, tuples and dictionaries are limited to sys.maxsize elements, so if the called function uses *args and/or **kwargs catch-all parameters then those are limited.

For the *args and **kwargs call syntax (expanding arguments) there are no limits other than the same sys.maxint size limits on Python standard types.

In versions before Python 3.7, CPython has a limit of 255 explicitly passed arguments in a call:

>>> def f(*args, **kwargs): pass
...
>>> exec("f({})".format(', '.join(map(str, range(256)))))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
SyntaxError: more than 255 arguments

This limitation is in place because until Python 3.5, the CALL_FUNCTION opcode overloaded the opcode argument to encode both the number of positional and keyword arguments on the stack, each encoded in a single byte.

Leave a Comment