简体   繁体   English

在Python 2中创建input()的理由是什么?

[英]What was the rationale behind creating input() in Python 2?

In Python 2, there were two ways of getting input. 在Python 2中,有两种获取输入的方法。 raw_input() and input() , which was a wrapper around eval(raw_input()) . raw_input()input() ,它是eval(raw_input())的包装器。 In Python 3 however, input() replaced raw_input() and the old meaing of input() was deprecated. 但是在Python 3中, input()取代了raw_input()并且不推荐使用input()的旧版本。 This is documented in What's new in Python 3 : 在Python 3中的新功能中有记录:

PEP 3111: raw_input() was renamed to input() . PEP 3111: raw_input()被重命名为input() That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. 也就是说,新的input()函数从sys.stdin读取一行并返回它,并删除尾随的换行符。 It raises EOFError if the input is terminated prematurely. 如果输入提前终止,它会EOFError To get the old behavior of input() , use eval(input()) . 要获取input()的旧行为,请使用eval(input())

But why exactly was input() around in Python 2 in the first place? 但是为什么在Python 2中首先是input()呢? What was the rationale for having user input that was evaluated as literal Python 2 code? 将用户输入评估为文字Python 2代码的理由是什么? This is what the Python 2 documentation had to say: 这就是Python 2文档所说的内容:

[ input() is] Equivalent to eval(raw_input(prompt)) . [ input() is]等效于eval(raw_input(prompt))

This function does not catch user errors. 此功能不会捕获用户错误。 If the input is not syntactically valid, a SyntaxError will be raised. 如果输入语法无效,则会引发SyntaxError Other exceptions may be raised if there is an error during evaluation. 如果评估期间出现错误,可能会引发其他异常。

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features . 如果加载了readline模块,则input()将使用它来提供精细的行编辑和历史记录功能

Consider using the raw_input() function for general input from users. 考虑使用raw_input()函数来获取用户的常规输入。

Notice the part in bold (which I emphasized). 注意粗体部分(我强调)。 What exactly does this mean? 这到底是什么意思? I looked over the documentation for the readline module and found a few things. 我查看了readline模块的文档并找到了一些内容。 The only real relevant bit I found, however, was this: 然而,我找到的唯一真正的相关位是:

Settings made using this module affect the behavior of both the interpreter's interactive prompt and the prompts offered by the raw_input() and input() built-in functions. 使用此模块进行的设置会影响解释器的交互式提示和raw_input()input()内置函数提供的提示的行为。

Which doesn't really help explain why input() was created or needed in the first place, though. 但这并没有真正帮助解释为什么首先创建或需要input()

Needless to say, using eval(any_user_input()) is very dangerous security wise, can cause debugging difficulties, and, from what I've read, is slow. 毋庸置疑,使用eval(any_user_input())是非常危险的安全方面,可能会导致调试困难,而且从我读过的内容来看,速度很慢。 So why did they create input() in Python 2 in the first place? 那么为什么他们首先在Python 2中创建input()呢? Were the developers unaware at the time of the downfalls of input() ? 开发人员在input()失败时是否意识不到?

References: 参考文献:

First of all, probably the only person who can answer this question decisively is the BDFL . 首先,可能唯一能够果断回答这个问题的人是BDFL

input can be useful in programs that are meant to be used by a programmer, so that they can enter complex structures, like {'foo': 42} , or even expressions, but less so in programs intended to be used by an unskilled user. input在程序员可以使用的程序中非常有用,因此它们可以输入复杂的结构,例如{'foo': 42} ,甚至是表达式,但是在不熟练的用户打算使用的程序中则更少。

From the SCM history we can see that both input and raw_input were present in 1990; 从SCM历史中我们可以看到inputraw_input 都出现在1990年; or pre-0.9 , when Python was in its infancy - back then exec was a function, and int('42') would have thrown an exception. 或者在0.9之前 ,当Python处于起步阶段时 - 然后exec是一个函数,而int('42')会抛出一个异常。 Most notably, eval was already present as well, so one could have used eval(raw_input()) even back then to get much of the same effect. 最值得注意的是, eval已经存在,所以人们甚至可以使用eval(raw_input())来获得更多相同的效果。

Back then there was no Zen of Python yet, and the "only one obvious way" wasn't as much a guiding principle, so this could have been an oversight. 那时候还没有Python的禅 ,而“唯一一种显而易见的方式”并不是一个指导原则,所以这可能是一种疏忽。

And both raw_input and input remained. raw_inputinput保持不变。 During the history of Python, the backwards-compatibility was a guiding principle, so input was unchanged until backwards-incompatible Python 3 was released. 在Python的历史中,向后兼容性是一个指导原则,因此input保持不变,直到向后兼容的Python 3发布。


As for the bolded part about readline module: if you import readline , then you can use arrow keys to move cursor keys around on the input() line, and configurable bindings; 关于readline模块的粗体部分:如果导入readline ,则可以使用箭头键在input()行上移动光标键,以及可配置的绑定; if readline is not imported in the program, then no such behaviour exists. 如果未在程序中导入readline ,则不存在此类行为。

Again, this wouldn't have been the reason for input existing in the first place; 同样,这也不会一直为理由input现有的摆在首位; back in 1990 Python didn't support such editing at all , regardless of whether input or raw_input was used. 早在1990年的Python并不支持这种编辑可言 ,无论inputraw_input使用。

For what it worths, input builtin was there in a first available Python version (0.9.1), it is from 1991. I can imagine Python 2.x had it for backwards compatibility with Python 1.x, and Python 1.x had it for backwards compatibility with 0.x. 对于它的价值, input内置是在第一个可用的Python版本(0.9.1),它是从1991年。我可以想象Python 2.x有它与Python 1.x向后兼容,而Python 1.x有它向后兼容0.x.

Say no to 0.x -> 1.x and 1.x -> 2.x porting issues! 拒绝0.x - > 1.x和1.x - > 2.x移植问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM