4

In Python 2, there were two ways of getting input. raw_input() and input(), which was a wrapper around eval(raw_input()). In Python 3 however, input() replaced raw_input() and the old meaing of input() was deprecated. This is documented in 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()).

But why exactly was input() around in Python 2 in the first place? What was the rationale for having user input that was evaluated as literal Python 2 code? This is what the Python 2 documentation had to say:

[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.

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. 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.

Which doesn't really help explain why input() was created or needed in the first place, though.

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. So why did they create input() in Python 2 in the first place? Were the developers unaware at the time of the downfalls of input()?

References:

Community
  • 1
  • 1
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
  • 2
    IMHO, the main motivation was to enable direct input of Python-style data (numbers, lists, dicts). – Leon Nov 15 '16 at 08:15
  • 2
    [This guy](http://stackoverflow.com/users/818274/guido-van-rossum) here can clear any confusions about the design decisions :-) – Dimitris Fasarakis Hilliard Nov 15 '16 at 08:57
  • 4
    It was convenient, it was early, and people weren't thinking long and hard about the security concerns at the time. Same reason C had `gets`. – user2357112 Nov 15 '16 at 09:01

2 Answers2

4

First of all, probably the only person who can answer this question decisively is the 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.

From the SCM history we can see that both input and raw_input were present in 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. 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.

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.

And both raw_input and input remained. During the history of Python, the backwards-compatibility was a guiding principle, so input was unchanged until backwards-incompatible Python 3 was released.


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; if readline is not imported in the program, then no such behaviour exists.

Again, this wouldn't have been the reason for input existing in the first place; back in 1990 Python didn't support such editing at all, regardless of whether input or raw_input was used.

Community
  • 1
  • 1
1

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.

Say no to 0.x -> 1.x and 1.x -> 2.x porting issues!

Mikhail Korobov
  • 21,908
  • 8
  • 73
  • 65
  • So why was it in 0.9.1? This doesn't really clarify anything. – TigerhawkT3 Nov 15 '16 at 08:24
  • The question was why is it in Python 2, I'm answering it. – Mikhail Korobov Nov 15 '16 at 08:45
  • 2
    The question was pretty obviously "why `input()` was created or needed in the first place." Sometimes, technically correct is not the best kind of correct. Do you really think this answer will satisfy _anyone_? Honestly? – TigerhawkT3 Nov 15 '16 at 08:55
  • 1
    I'm getting the impression that, the question being about an apparently useless function, you posted this as some kind of joke about things that people know are useless and yet publish anyway... – TigerhawkT3 Nov 15 '16 at 09:10
  • 1
    Well, an obvious answer to "why input() was created in a first place" is that someone wanted to eval user input. This can be natural in early stages of development of a scripting language, maybe it was a poor man's REPL. People make bad decisions all the time; early versions of *everything* contain a lot of useless and incorrect stuff. IMHO the interesting part of a question is why it survived for so many years. So for me "why is it in Python 2" was more interesting than "why was it created in a first place". – Mikhail Korobov Nov 15 '16 at 09:37
  • So you're making the case that `input()` existed only to satisfy backwards compatibility with older versions of the language? Do you have any links of reference to support this argument? – Christian Dean Nov 15 '16 at 12:53
  • A good question; no, I don't have references unfortunately, I can't find any online discussions about that. – Mikhail Korobov Nov 15 '16 at 15:40