[Tutor] What's the difference between raw_input and input?
Brian van den Broek
bvande at po-box.mcgill.ca
Mon Sep 20 20:36:42 CEST 2004
Dick Moores said unto the world upon 2004-09-20 14:12:
> Bob Gailer wrote at 09:00 9/20/2004:
>> At 09:12 AM 9/20/2004, W X Liu wrote:
>>> Hi all,
>>> Does anybody know What's the difference between raw_input and input?
>> Have you looked them up in the Python Reference? I suggest you do so.
>> We like to help, but we hesitate to spend valuable time answering
>> questions that can be easily looked up. If you do not understand what
>> the reference says, come back and tell us what the problem is.
> The question made me realize I didn't know the difference either. My
> attempts to get the answer turned up only this description of raw_imput():
> raw_input( [prompt])
> 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. Example:
> >>> s = raw_input('--> ')
> --> Monty Python's Flying Circus
> >>> s
> "Monty Python's Flying Circus"
> If the readline module was loaded, then raw_input() will use it to
> provide elaborate line editing and history features.
> But nothing about input() in the "Python 2.3 Documentation" relevant to
> the question. But I probably don't yet know my way perfectly around the
> Dick Moores
Hey Dick, W X Liu, and all,
To elaborate on what Olavi Ivask said up-thread and from the docs:
Equivalent to eval(raw_input(prompt)). Warning: This function is not
safe from user errors! It expects a valid Python expression as input; if
the input is not syntactically valid, a SyntaxError will be raised.
Other exceptions may be raised if there is an error during evaluation.
(On the other hand, sometimes this is exactly what you need when writing
a quick script for expert use.)
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.
The point is that raw_input() returns a string obtained from the user,
whereas input() evaluates (as in -- more or less -- runs) a string
obtained from the user assuming it is valid Python code. The two
pitfalls of input() pointed out are:
1) Exceptions are raised for non-well-formed Python expressions, and,
2) Well formed Python expressions can wreak havoc. The user could, after
all, type a string consisting of the Python expressions to delete all
files from C:\Windows. IMHO, you certainly wouldn't want to give a
script using input() to someone with enough knowledge to be dangerous
and a habit of typing stuff "to see what happens".
I've yet to come across a situation where I'd want to use input(). And,
even though I'm the only user of my scripts, I don't trust the 4am me
enough to ask me(him?) for arbitrary code to execute. (Particularly
since I now know enough to be dangerous.) This way lies tears ;-)
Best to all,
More information about the Tutor