On 1/30/2014 12:13 AM, Gregory Ewing wrote:
Steven D'Aprano wrote:
On Mon, 27 Jan 2014 12:22:22 -0800, Rick Johnson wrote:

Why do we even need an "input" function anyway if all it is going to do
is read from stdin?

That's not all it does.

What else it does is print a prompt before reading and to strip off the trailing newline.

For example, it handles backspacing, so that typing H E L O O
BACKSPACE BACKSPACE L O gives "HELLO" rather than "HELOO\x7f\x7fO".

No, it doesn't -- that's handled at a lower level.
Any other method of reading from stdin, as long
as it hasn't been redirected away from the console,
has the same behaviour.

I typed some backspaces in the input to each of the
following experiments, and they didn't end up in the
data:

 >>> import sys
 >>> x = sys.stdin.readline()
HELLO
 >>> x
'HELLO\n'
 >>> import os
 >>> f = os.fdopen(0)
 >>> y = f.readline()
adsxx
 >>> y
'adsxx\n'

So input() really is a pure convenience function.
(That doesn't mean it's not worth having, though!)

It is equivalent to

def input(prompt):
  sys.stdout.write(prompt)
  return sys.stdin.read(<one line>)[:-1]

There was once an eval around the return, but that was determined to be a bad idea.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to