return None
or return
if I want to end the function processing with a return statement?When I was writing a Python function, I suddenly wondered.
The question is whether you should write return
or return None
if you want to end the process in the middle of the function.
Since it is recommended to write it explicitly in Python, I was wondering if I should write return None
when ending the process with the return statement and when returning None
. (* In Python, if only return
is written in the function definition, or if there is no description of return
, None
will be returned. Reference: [Python] Value returned by the function that did not describe return" )
From the conclusion, it seems that ** returnis better ** when you just want to end the function. However, there seem to be some things to be aware of when writing only
return`.
return, return None, and no return at all? was helpful. Let me explain with an example.
return None
def hello_all():
persons = ['Zepp', 'Eric']
if persons:
for person in persons:
print('Hello ' + person)
#Returns None if the list is empty
else:
return None
return
If you want to use the return value None, you explicitly wrote return None
, but if you want to end the function instead of using the return value, you can use return
. The code below uses it to exit the for loop and terminate the function.
def hello_zepp():
persons = ['Zepp', 'Eric']
for person in persons:
print('Hello ' + person)
#End processing
return
In other words, when the ** for loop is single, it works the same as break
**.
If you want to end the process in a for loop nested in multiple for loops, unlike break
, you can end the function by writing only one return
, which simplifies the code.
When using return
in this way, use it when you want to forcibly end the process in the middle of the function, such as when exiting the loop.
In this case, the return value None
is not supposed to be used, so you should not assign the return value to a variable.
#↓ should not be done
result = hello_zepp()
Also, if you want to end the function in the middle, you may want to interrupt the process because the conditions are not met. In such a case, it is better to create an appropriate exception class, throw the exception, and handle the exception at the caller, rather than using return
.
return
Python functions do not necessarily require a return statement, but even if there is no return statement, None
is returned as the return value.
#There is no return statement, but None is returned as the return value
def hello_eric():
persons = ['Zepp', 'Eric']
print('Hello ' + persons[1])
In this case as well, as in the case of return None
, this None
is not supposed to be used, so do not assign the return value to a variable.
--It is better to use return
when you just want to interrupt the function processing.
--Use return None
to use the return value None
.
--return
may be useful when you want to get out of multiple loops at once.
--Do not use return
or the return value None
without a return statement.
Recommended Posts