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!

6th Oct 2020, 11:39 AM
Adam Haller
Adam Haller - avatar
3 odpowiedzi
+ 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())
6th Oct 2020, 11:41 AM
Slick
Slick - avatar
+ 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.
6th Oct 2020, 2:32 PM
Lothar
Lothar - avatar
+ 1
def somefunc(): return "Hi!" print(somefunc())
6th Oct 2020, 2:14 PM
Shadoff
Shadoff - avatar