input()函数的输入有多大? [英] How big can the input to the input() function be?

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

问题描述

我输入给 input()函数的输入有多大?

不幸的是,没有简单的方法来测试它。在使用大量复制粘贴后,我无法获取 input 在我提供的任何输入时失败。 (我最终放弃了)



input 函数的noreferrer>文档没有提及任何关于此的内容:


如果存在提示符参数,则将其写入标准输出而没有尾随换行符。然后该函数从输入中读取一行,将其转换为一个字符串(剥离尾随的换行符),然后返回该行。当读取EOF时,会引发 EOFError


所以,我猜这是没有限制的?有谁知道是否有,如果有的话,它有多少?

解决方案

当然有,它不可能是无限的*。我认为需要突出显示的文档中的关键句是:
$ b


[...]然后函数从输入中读取一行, strong>将其转换为字符串(剥离尾随换行符)[...]

(强调mine)

因为它将您提供的输入转换为 Python str 有小于或等于Python可以创建的最大字符串。



没有给出明确大小的原因可能是因为这是一个实现细节。对Python的所有其他实现强制执行最大大小没有多大意义。

*在CPython中,至少,字符串的最大大小受其允许的索引的大小限制(请参阅 PEP 353 )。也就是说,当你尝试索引它时,方括号 [] 中的数字有多大:

 >>> s =''
>>> s [2 ** 63]

IndexErrorTraceback(最近一次调用最后一次)
< ipython-input-10-75e9ac36da20> in< module>()
----> 1 s [2 ** 63]

IndexError:无法将'int'放入索引大小的整数

(尝试前面的 2 ** 63 - 1 ,这是积极的可接受限制, -2 ** 63 是负数限制。)



对于指数,它不是内部使用的Python数字;相反,它是一个 Py_ssize_t ,它分别是32/64位机器上的带符号32/64位int。所以,这就是它看起来的硬性限制。



(因为错误消息指出,int和 intex大小的整数是两种不同的东西)



它也像 PY_SSIZE_T_MAX input(),则显式检查 (最大尺寸 Py_ssize_t ):

 (len> PY_SSIZE_T_MAX){
PyErr_SetString(PyExc_OverflowError,
input:input too long);
result = NULL;





$ p然后它将输入转换为Python str with PyUnicode_Decode






从你的角度来看;如果平均书是 500.000 字符长,并且估计为总数约为1.3亿,理论上你可以 input 周围:

 >>> ((2 ** 63)-1)// 500000 * 130000000 
141898

次那些角色;它可能需要一些时间,但:-)(你会受到可用内存的限制!)


How large can the input I supply to the input() function be?

Unfortunately, there was no easy way to test it. After using a lot of copy-pasting I couldn't get input to fail on any input I supplied. (and I eventually gave up)

The documentation for the input function doesn't mention anything regarding this:

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

So, I'm guessing there is no limit? Does anyone know if there is and, if so, how much is it?

解决方案

Of course there is, it can't be limitless*. The key sentence from the documentation that I believe needs highlighting is:

[...] The function then reads a line from input, converts it to a string (stripping a trailing newline) [...]

(emphasis mine)

Since it converts the input you supply into a Python str object it essentially translates to: "Its size has to be less than or equal to the largest string Python can create".

The reason why no explicit size is given is probably because this is an implementation detail. Enforcing a maximum size to all other implementations of Python wouldn't make much sense.

*In CPython, at least, the largest size of a string is bounded by how big its index is allowed to be (see PEP 353). That is, how big the number in the brackets [] is allowed to be when you try and index it:

>>> s = ''
>>> s[2 ** 63]

IndexErrorTraceback (most recent call last)
<ipython-input-10-75e9ac36da20> in <module>()
----> 1 s[2 ** 63]

IndexError: cannot fit 'int' into an index-sized integer

(try the previous with 2 ** 63 - 1, that's the positive acceptable limit, -2 ** 63 is the negative limit.)

For indices, it isn't Python numbers that are internally used; instead, it is a Py_ssize_t which is a signed 32/64 bit int on 32/64 bit machines respectively. So, that's the hard limit from what it seems.

(as the error message states, int and intex-sized integer are two different things)

It also seems like input() explicitly checks if the input supplied is larger than PY_SSIZE_T_MAX (the maximum size of Py_ssize_t) before converting:

if (len > PY_SSIZE_T_MAX) {
    PyErr_SetString(PyExc_OverflowError,
                    "input: input too long");
    result = NULL;
}

Then it converts the input to a Python str with PyUnicode_Decode.


To put that in perspective for you; if the average book is 500.000 characters long and the estimation for the total number of books is around 130 million, you could theoretically input around:

>>> ((2 ** 63) - 1) // 500000 * 130000000
141898

times those characters; it would probably take you some time, though :-) (and you'd be limited by available memory first!)

这篇关于input()函数的输入有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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