seeker seeker - 2 months ago 6
Python Question

Why does this function return this value?

I have defined a function using Python as follows:

def output ():
print "Hello, world!"

output ()


When this script is run in windows command line it will return:

Hello, world!


What I would like clarified is: why this is? Why is a value output despite there having been no return value defined?

Answer

The default return of the method will be None. If you perform the following:

res = output()

and print out res, it will be None. By default, None is always returned when not specifying a return.

>>> def output():
...     print "Hello, world!"
...
>>> res = output()
Hello, world!
>>> res
>>> type(res)
<type 'NoneType'>

However, you will still get something printed to screen, because the print statement will run when the method is called.