[Python] Which should be used, return or return None

Should I use 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 onlyreturn`.

Let me give you an example

return, return None, and no return at all? was helpful. Let me explain with an example.

Use 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

Use 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.

: no_good: Be careful

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.

No description of 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.

Conclusion

--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

[Python] Which should be used, return or return None
Which should I study, R or Python, for data analysis?
Which is better, PyPy or Python?
[Python] return A [or / and] B
33 strings that should not be used as variable names in python
Which is faster, Python shuffle or sample?
++ and-cannot be used for increment / decrement in python
Python early return
Python note: When the pip command cannot be used
[Python] Boolean operator (or / and) does not return Boolean value
Japanese can be used with Python in Docker environment
Python knowledge notes that can be used with AtCoder
[Memorandum] Japanese keys cannot be used in python string.Template.substitute
list comprehension because operator.methodcaller cannot be used in python 2.5
Operators ++,-cannot be used in python (difference from php)
Can be used in competition pros! Python standard library
Difference between return, return None, and no return description in Python
python> check NoneType or not> if a == None:> if a is None:
Which is better, python standard input receiving input () or sys.stdin?
[Redash] Standard library cannot be used in python function