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

Soviut picture Soviut · Apr 3, 2009 · Viewed 27.4k times · Source

It's somewhat common knowledge that Python functions can have a maximum of 256 arguments. What I'm curious to know is if this limit applies to *args and **kwargs when they're unrolled in the following manner:

items = [1,2,3,4,5,6]

def do_something(*items):
    pass

I ask because, hypothetically, there might be cases where a list larger than 256 items gets unrolled as a set of *args or **kwargs.

Answer

vartec picture vartec · Apr 3, 2009

WFM

>>> fstr = 'def f(%s): pass' % (', '.join(['arg%d' % i for i in range(5000)]))
>>> exec(fstr)
>>> f
<function f at 0x829bae4>

Update: as Brian noticed, the limit is on the calling side:

>>> exec 'f(' + ','.join(str(i) for i in range(5000)) + ')'

Traceback (most recent call last):
  File "<pyshell#63>", line 1, in <module>
    exec 'f(' + ','.join(str(i) for i in range(5000)) + ')'
  File "<string>", line 1
SyntaxError: more than 255 arguments (<string>, line 1)

on the other hand this works:

>>> f(*range(5000))
>>> 

Conclusion: no, it does not apply to unrolled arguments.