在 Python 2 中创建 input() 背后的基本原理是什么?
What was the rationale behind creating input() in Python 2?
在Python2中,有两种获取输入的方式。 raw_input()
和 input()
,这是 eval(raw_input())
的包装。然而,在 Python 3 中,input()
取代了 raw_input()
并且 input()
的旧含义已被弃用。这记录在 What's new in Python 3:
PEP 3111: raw_input()
was renamed to input()
. That is, the new input()
function reads a line from sys.stdin
and returns it with the trailing newline stripped. It raises EOFError
if the input is terminated prematurely. To get the old behavior of input()
, use eval(input())
.
但为什么 input()
一开始就在 Python 2 附近?将用户输入评估为文字 Python 2 代码的基本原理是什么?这是 Python 2 文档不得不说的:
[input()
is] Equivalent to eval(raw_input(prompt))
.
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError
will be raised. 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.
Consider using the raw_input()
function for general input from users.
注意粗体部分(我强调的部分)。这到底是什么意思? I looked over the documentation for the readline
module 并发现了一些东西。然而,我发现的唯一真正相关的部分是:
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.
不过,这并不能真正帮助解释为什么首先要创建或需要 input()
。
不用说,使用 eval(any_user_input())
在安全方面是非常危险的,会导致调试困难,而且据我所知,速度很慢。那么他们为什么首先在 Python 2 中创建 input()
呢?开发人员在 input()
倒台时没有意识到吗?
参考文献:
- Is using eval in Python a bad practice?
- eval(input()) in python 2to3
- What’s New In Python 3.0
- Python 2.7.12 documentation
就其价值而言,input
内置函数存在于第一个可用的 Python 版本 (0.9.1) 中,它是从 1991 年开始的。我可以想象 Python 2.x 向后兼容 Python 1.x,Python 1.x 向后兼容 0.x。
对 0.x -> 1.x 和 1.x -> 2.x 移植问题说不!
首先,能果断回答这个问题的大概只有the BDFL.
input
在旨在供程序员使用的程序中很有用,因此他们可以输入复杂的结构,如 {'foo': 42}
,甚至是表达式,但在程序中则不太有用供不熟练的用户使用。
从SCM历史我们可以看到input
和raw_input
were present in 1990; or pre-0.9,当时Python还处于起步阶段——当时exec
是一个函数,而 int('42')
会抛出异常。最值得注意的是,eval
也已经存在,因此即使在那时也可以使用 eval(raw_input())
来获得相同的效果。
那时候还没有 Zen of Python,"only one obvious way" 也不是指导原则,所以这可能是一个疏忽。
并且 raw_input
和 input
都保留了下来。在 Python 的历史中,向后兼容是一个指导原则,因此 input
一直保持不变,直到向后不兼容的 Python 3 发布。
关于 readline
模块的粗体部分:如果您导入 readline
,那么您可以使用箭头键在 input()
行上移动光标键,以及可配置的绑定;如果程序中未导入 readline
,则不存在此类行为。
同样,这并不是 input
存在的原因;早在 1990 年,Python 根本不支持这种编辑 ,无论使用的是 input
还是 raw_input
。
在Python2中,有两种获取输入的方式。 raw_input()
和 input()
,这是 eval(raw_input())
的包装。然而,在 Python 3 中,input()
取代了 raw_input()
并且 input()
的旧含义已被弃用。这记录在 What's new in Python 3:
PEP 3111:
raw_input()
was renamed toinput()
. That is, the newinput()
function reads a line fromsys.stdin
and returns it with the trailing newline stripped. It raisesEOFError
if the input is terminated prematurely. To get the old behavior ofinput()
, useeval(input())
.
但为什么 input()
一开始就在 Python 2 附近?将用户输入评估为文字 Python 2 代码的基本原理是什么?这是 Python 2 文档不得不说的:
[
input()
is] Equivalent toeval(raw_input(prompt))
.This function does not catch user errors. If the input is not syntactically valid, a
SyntaxError
will be raised. Other exceptions may be raised if there is an error during evaluation.If the
readline
module was loaded, theninput()
will use it to provide elaborate line editing and history features.Consider using the
raw_input()
function for general input from users.
注意粗体部分(我强调的部分)。这到底是什么意思? I looked over the documentation for the readline
module 并发现了一些东西。然而,我发现的唯一真正相关的部分是:
Settings made using this module affect the behavior of both the interpreter’s interactive prompt and the prompts offered by the
raw_input()
andinput()
built-in functions.
不过,这并不能真正帮助解释为什么首先要创建或需要 input()
。
不用说,使用 eval(any_user_input())
在安全方面是非常危险的,会导致调试困难,而且据我所知,速度很慢。那么他们为什么首先在 Python 2 中创建 input()
呢?开发人员在 input()
倒台时没有意识到吗?
参考文献:
- Is using eval in Python a bad practice?
- eval(input()) in python 2to3
- What’s New In Python 3.0
- Python 2.7.12 documentation
就其价值而言,input
内置函数存在于第一个可用的 Python 版本 (0.9.1) 中,它是从 1991 年开始的。我可以想象 Python 2.x 向后兼容 Python 1.x,Python 1.x 向后兼容 0.x。
对 0.x -> 1.x 和 1.x -> 2.x 移植问题说不!
首先,能果断回答这个问题的大概只有the BDFL.
input
在旨在供程序员使用的程序中很有用,因此他们可以输入复杂的结构,如 {'foo': 42}
,甚至是表达式,但在程序中则不太有用供不熟练的用户使用。
从SCM历史我们可以看到input
和raw_input
were present in 1990; or pre-0.9,当时Python还处于起步阶段——当时exec
是一个函数,而 int('42')
会抛出异常。最值得注意的是,eval
也已经存在,因此即使在那时也可以使用 eval(raw_input())
来获得相同的效果。
那时候还没有 Zen of Python,"only one obvious way" 也不是指导原则,所以这可能是一个疏忽。
并且 raw_input
和 input
都保留了下来。在 Python 的历史中,向后兼容是一个指导原则,因此 input
一直保持不变,直到向后不兼容的 Python 3 发布。
关于 readline
模块的粗体部分:如果您导入 readline
,那么您可以使用箭头键在 input()
行上移动光标键,以及可配置的绑定;如果程序中未导入 readline
,则不存在此类行为。
同样,这并不是 input
存在的原因;早在 1990 年,Python 根本不支持这种编辑 ,无论使用的是 input
还是 raw_input
。