0
Python: None
I searched for the exact question but still can't understand.... def some_func(): print("Hi!") var = some_func() print(var) the output : Hi! None Where did None came from? I learnt about functions before this doesn't do like this... Please explain like im five... Thanks in advance!
3 Respostas
+ 4
because you print Hi! in the function. either just call:
some_func() # NO print
or in the function, RETURN "Hi!" instead of printing it. Then you can call:
print(some_func())
+ 3
The reason is:
All python functions do return the value None except there is an explicit return statement written with a value other than None.
The fact that None is returned, is NOT related with the print() statement in the function. if you replace this print statement like this:
def some_func():
"hello"
the function will return None anyway.
+ 1
def somefunc():
return "Hi!"
print(somefunc())