Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 963
Soviut Avatar asked Apr 03 '09 15:04

Soviut


People also ask

What is the maximum number of arguments in function?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

How many function arguments is too many Python?

Functions with three arguments (triadic function) should be avoided if possible. More than three arguments (polyadic function) are only for very specific cases and then shouldn't be used anyway.

How many arguments can Python take?

5 Types of Arguments in Python Function Definition: keyword arguments. positional arguments. arbitrary positional arguments. arbitrary keyword arguments.

Can Python take an unlimited number of arguments?

Yes. You can use *args as a non-keyword argument. You will then be able to pass any number of arguments. As you can see, Python will unpack the arguments as a single tuple with all the arguments.


2 Answers

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.

like image 177
vartec Avatar answered Oct 02 '22 19:10

vartec


In Python 3.6 and before, the limit is due to how the compiled bytecode treats calling a function with position arguments and/or keyword arguments.

The bytecode op of concern is CALL_FUNCTION which carries an op_arg that is 4 bytes in length, but on the two least significant bytes are used. Of those, the most significant byte represent the number of keyword arguments on the stack and the least significant byte the number of positional arguments on the stack. Therefore, you can have at most 0xFF == 255 keyword arguments or 0xFF == 255 positional arguments.

This limit does not apply to *args and **kwargs because calls with that grammar use the bytecode ops CALL_FUNCTION_VAR, CALL_FUNCTION_KW, and CALL_FUNCTION_VAR_KW depending on the signature. For these opcodes, the stack consists of an iterable for the *args and a dict for the **kwargs. These items get passed directly to the receiver which unrolls them as needed.

like image 31
Chris Colbert Avatar answered Oct 02 '22 20:10

Chris Colbert