I'm viewing the source code of a project now. In which I saw a function like this.
def func(x):
if condition_a:
return
if condition_b:
return
process_something
What does this return with nothing do here?
I'm viewing the source code of a project now. In which I saw a function like this.
def func(x):
if condition_a:
return
if condition_b:
return
process_something
What does this return with nothing do here?
The "return
with nothing" exits the function at that line and returns None
. As Python's docs say:
If an expression list is present, it is evaluated, else
None
is substituted.
return
leaves the current function call with the expression list (orNone
) as return value.
In flow control, I've seen it commonly used when some sort of condition is encountered that makes it impossible to execute the rest of the function; for example,
def getDataFromServer():
if serverNotResponding():
return
# do important stuff here which requires the server to be running