Python 函数中的最大参数数是多少? [英] What is a maximum number of arguments in a Python function?

查看:26
本文介绍了Python 函数中的最大参数数是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 函数最多可以有 256 个参数,这在一定程度上是常识.我很想知道的是,当 *args**kwargs 以下列方式展开时,此限制是否适用于它们:

items = [1,2,3,4,5,6]def do_something(*items):经过

我之所以这么问是因为,假设可能存在超过 256 个项目的列表作为一组 *args**kwargs 展开的情况.

解决方案

在 Python 3.7 和更新版本中,没有限制.这是在 issue #27213issue #12844;#27213 重新设计了 CALL_FUNCTION* 操作码系列以提高性能和简单性(3.6 的一部分),释放操作码参数以仅编码单个参数计数,并且 #12844 删除了阻止编译带有更多参数的代码.

从 3.7 开始,使用 EXTENDED_ARG() 操作码,现在完全没有限制你可以使用显式参数传递多少个参数,保存多少可以装入堆栈(所以现在由你的记忆):

<预><代码>>>>导入系统>>>sys.version_infosys.version_info(major=3,minor=7,micro=0,releaselevel='alpha',serial=2)>>>def f(*args, **kwargs): 通过...>>>exec("f({})".format(', '.join(map(str, range(256)))))>>>exec("f({})".format(', '.join(map(str, range(2 ** 16)))))

请注意,列表、元组和字典仅限于 sys.maxsize 元素,所以如果被调用的函数使用 *args 和/或 **kwargs 捕获所有参数,那么那些 有限.

对于 *args**kwargs 调用语法(扩展参数),除了相同的 sys.maxint 大小之外没有任何限制Python 标准类型的限制.

在 Python 3.7 之前的版本中,CPython 在调用中明确传递的参数限制为 255 个:

<预><代码>>>>def f(*args, **kwargs): 通过...>>>exec("f({})".format(', '.join(map(str, range(256)))))回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中文件",第 1 行语法错误:超过 255 个参数

这个限制是存在的,因为在 Python 3.5 之前,CALL_FUNCTION opcode 重载了 opcode 参数以对堆栈上的位置参数和关键字参数的数量进行编码,每个参数都编码为一个字节.

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.

解决方案

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.

这篇关于Python 函数中的最大参数数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆