-2
def other_new_function():
    print "HELLO"

print "Start", other_new_function(), "Stop"

The output of this program is:

Start HELLO

None Stop

Why is NONE being showed in the output?

4

2 に答える 2

4

Because you're printing "HELLO", and not returning it.

Because your function does not return anything, it defaults to returning None. Hence, the None that appears.

Change:

def other_new_function():
    print "HELLO"

To:

def other_new_function():
    return "HELLO"

There's a big difference to returning and printing something from a function:

>>> def func():
...     print 1
... 
>>> def func2():
...     return 1
... 
>>> myvar = func()
1 # Printed
>>> myvar2 = func2() # Returned to a variable
>>> print myvar
None
>>> print myvar2
1

Because function 1 prints the value, it never gets returned to a variable.

于 2013-10-19T05:18:58.650 に答える
0
def other_new_function():
    print "HELLO"

NoneType明示的に値を返さないため、(「なし」として出力されます) を返します。おそらくやりたいことは次のとおりです。

def other_new_function():
    return "HELLO"
于 2013-10-19T05:20:21.137 に答える