When saving a piece of data, whatever its type, what advantages does input have over sys.stdin and/or vice versa? From what I understand, the result is the same.
When saving a piece of data, whatever its type, what advantages does input have over sys.stdin and/or vice versa? From what I understand, the result is the same.
The standard streams are
stdin
,stdout
andstderr
, and they represent files connected to the default input, output, and error output of a process. Our application must not assume anything about them, neither that the input is done by keyboard, nor that the output goes to a terminal,...nothing, just that they behave like text files that are read and/or written line by line. line.The function
input
has two tasks:stdout
)stdin
)This behavior can be simulated as follows:
Which is equivalent to
Obviously, it is much more comfortable to use
input
than the standard streams (The same goes for usingprint
instead ofstdout
). So what is the point of using streams?As I said before, the streams can be seen as files and can be exchanged with them where necessary. It is a simple system for creating test suites from code snippets. It is also used to process the output or input of child processes (something that is off topic).
A non-trivial example:
It doesn't wait for you to enter the number on the keyboard, it reads the one
"2"
stored inStringIO
.Use
input
orstdin
is equivalent. It's more comfortable to useinput
in almost any condition, butstdin
you can do a lot more magic with it.