seeker seeker - 1 year ago 79
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download