Say we have run this line of code in an Python or IDLE shell:
print("hai")
The below appears on screen:
hai
This just seems counterintuitive to me, since print(arg) is a void function and thus returns None when evaluated, always; NoneType is a datatype in python, yet it is not displayed on the window when an expression evaluates to it, while integers are:
5
makes this appear on screen (running on shell):
5
Is this non-displaying behaviour hard-coded into python to only be a feature of NoneType data?
I’m asking this question because there seems to be ambiguity in what is considered an “output,” and I’m worried this ambiguity is going to make my mid-terms pretty disastrous. For example, consider this line of code…
…run in shell:
5
makes this appear on screen:
5
…compiled and run:
5
Doesn't make anything appear on screen.
--
The expression 5 trivially evaluates to the integer 5 in Python, but is the integer 5 an output in the same way that...
print(5)
…outputs “5” on the screen? And how about the NoneType data object that the print(5) function call evaluates to; would that be an output too?
I guess the practical ramifications of my question would be this; say someone asks you this question:
What is the output of this line of code?:
print(5)
Would the correct answer be:
(a).
5
None
(b).
5
or
(c).
None ?
and for good measure, how about this question:
What is the output of this line of code?:
5
Would the correct answer be:
(a).
or
(b).
5
The answer to the two above questions depends on a couple things:
“What exactly constitutes an output? Is an evaluated value an output? Or are outputs literally just: what would the screen look like after I press the return key”
“Does how are we running the code matter? Since writing the line of code in TextEdit, compiling it, and then running it yields different things appearing on the screen than typing the line of code in the shell and hitting return."
This is a somewhat UNIX-slated description, but with a little tweaking applies to Windows as well.
Every process, when it first starts up, has three open file descriptors which are referred to by number. File descriptor 0 is open for reading, and is generally referred to as standard input. File descriptors 1 and 2 are open for writing, and are generally referred to as standard output and standard error, respectively.
By "output", we generally mean "something written to standard output". In the case of the print
function, its purpose is to write some data to an open file descriptor, which by default is standard output. Whether this is visible depends on which file is opened for standard output.
The interactive interpreter usually has your terminal as its standard output. (Remember, many things in Unix are represented as files, whether or not they are byte-addressable objects stored on a disk.) So when you execute print
, it writes something to your screen.
However, the interactive interpreter also writes the value of each expression you type at the prompt to standard output, which is what makes it an interactive interpreter as opposed to just an interpreter. Any expression foo
you type at the prompt is treated as print(foo)
. (The exception being if the return value is None
; as the only value of NoneType
, the fact that a function returns None
doesn't really provide any information, so it's considered unnecessary to print its value to the screen. That argument really
only applies if the function can only return None
, as there is clearly a difference between returning None
and, say, 1
, but as far as output is concerned, you can still see the difference between the interpreter putting 1 or nothing for foo(5)
when it returns 1
or None
.)
When deciding if something "has" output, it's better to type it into a file, and execute that file has a script. There won't be any implicit output from a simple expression like 3 + 5
; the script will evaluate that as a so-called expression statement, and produce but otherwise discard the result of 8.