Let’s assume an iteration in which we call a function without a return value. The way I think my program should behave is explained in this pseudocode:
JavaScript
x
11
11
1
for element in some_list:
2
foo(element)
3
4
def foo(element):
5
do something
6
if check is true:
7
do more (because check was succesful)
8
else:
9
return None
10
do much much more
11
If I implement this in python, it bothers me, that the function returns a None
. Is there a better way for “exiting a function, that has no return value, if a check fails in the body of the function”?
Advertisement
Answer
You could simply use
JavaScript
1
2
1
return
2
which does exactly the same as
JavaScript
1
2
1
return None
2
Your function will also return None
if execution reaches the end of the function body without hitting a return
statement. Returning nothing is the same as returning None
in Python.