什么是一个Python函数的参数最大数量? [英] What is a maximum number of arguments in a Python function?
本文介绍了什么是一个Python函数的参数最大数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有点常识,Python函数最多可以有256个参数。如果该限制适用于 * ARGS
和 ** kwargs
时,他们摊开我很好奇,想知道的是以下面的方式:
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
我问,因为,假设,因此可能会出现一个列表大于256项被展开为一组的情况下, * ARGS
或 ** kwargs
。
推荐答案
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.
这篇关于什么是一个Python函数的参数最大数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文