On the actual behavior, there is no difference. They all return None and that's it. However, there is a time and place for all of these. The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.

Using return None

This tells that the function is indeed meant to return a value for later use, and in this case it returns None. This value None can then be used elsewhere. return None is never used if there are no other possible return values from the function.

In the following example, we return person's mother if the person given is a human. If it's not a human, we return None since the person doesn't have a mother (let's suppose it's not an animal or something).

def get_mother(person):
    if is_human(person):
        return person.mother
    else:
        return None

Using return

This is used for the same reason as break in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.

We've got 15 prisoners and we know one of them has a knife. We loop through each prisoner one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the prisoners. If we don't find the prisoner with a knife, we raise an alert. This could be done in many different ways and using return is probably not even the best way, but it's just an example to show how to use return for exiting a function.

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()

Note: You should never do var = find_prisoner_with_knife(), since the return value is not meant to be caught.

Using no return at all

This will also return None, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.

In the following example, we set person's mother's name and then the function exits after completing successfully.

def set_mother(person, mother):
    if is_human(person):
        person.mother = mother

Note: You should never do var = set_mother(my_person, my_mother), since the return value is not meant to be caught.

Answer from user2032433 on Stack Overflow
🌐
Real Python
realpython.com › python-return-statement
The Python return Statement: Usage and Best Practices – Real Python
June 14, 2024 - Everything in Python is an object. So, your functions can return numeric values (int, float, and complex values), collections and sequences of objects (list, tuple, dictionary, or set objects), user-defined objects, classes, functions, and even modules or packages. You can omit the return value of a function and use a bare return without a return value.
Top answer
1 of 5
787

On the actual behavior, there is no difference. They all return None and that's it. However, there is a time and place for all of these. The following instructions are basically how the different methods should be used (or at least how I was taught they should be used), but they are not absolute rules so you can mix them up if you feel necessary to.

Using return None

This tells that the function is indeed meant to return a value for later use, and in this case it returns None. This value None can then be used elsewhere. return None is never used if there are no other possible return values from the function.

In the following example, we return person's mother if the person given is a human. If it's not a human, we return None since the person doesn't have a mother (let's suppose it's not an animal or something).

def get_mother(person):
    if is_human(person):
        return person.mother
    else:
        return None

Using return

This is used for the same reason as break in loops. The return value doesn't matter and you only want to exit the whole function. It's extremely useful in some places, even though you don't need it that often.

We've got 15 prisoners and we know one of them has a knife. We loop through each prisoner one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function because we know there's only one knife and no reason the check rest of the prisoners. If we don't find the prisoner with a knife, we raise an alert. This could be done in many different ways and using return is probably not even the best way, but it's just an example to show how to use return for exiting a function.

def find_prisoner_with_knife(prisoners):
    for prisoner in prisoners:
        if "knife" in prisoner.items:
            prisoner.move_to_inquisition()
            return # no need to check rest of the prisoners nor raise an alert
    raise_alert()

Note: You should never do var = find_prisoner_with_knife(), since the return value is not meant to be caught.

Using no return at all

This will also return None, but that value is not meant to be used or caught. It simply means that the function ended successfully. It's basically the same as return in void functions in languages such as C++ or Java.

In the following example, we set person's mother's name and then the function exits after completing successfully.

def set_mother(person, mother):
    if is_human(person):
        person.mother = mother

Note: You should never do var = set_mother(my_person, my_mother), since the return value is not meant to be caught.

2 of 5
57

Yes, they are all the same.

We can review the interpreted machine code to confirm that that they're all doing the exact same thing.

import dis

def f1():
  print "Hello World"
  return None

def f2():
  print "Hello World"
  return

def f3():
  print "Hello World"

dis.dis(f1)
    4   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    5   5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f2)
    9   0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE

    10  5 LOAD_CONST    0 (None)
        8 RETURN_VALUE

dis.dis(f3)
    14  0 LOAD_CONST    1 ('Hello World')
        3 PRINT_ITEM
        4 PRINT_NEWLINE            
        5 LOAD_CONST    0 (None)
        8 RETURN_VALUE      
🌐
Reddit
reddit.com › r/learnpython › is it bad practice to define functions with no return value?
r/learnpython on Reddit: Is it bad practice to define functions with no return value?
January 3, 2024 -

For example, I have a function that saves data as a csv file and this function does not have a return value.

I’m unsure as to whether or not this is bad practice? I could return 0 to indicate that the data saved successfully and 1 to denote otherwise, but I’ve already got Raise Error in the save function so this feels unnecessary.

🌐
Quora
quora.com › In-Python-what-does-return-None-mean-if-inside-a-function-we-use-return-without-any-value
In Python, what does return None mean if inside a function we use return without any value? - Quora
Answer (1 of 4): You’re correct; just a simple [code ]return[/code] statement without a value will return [code ]None[/code] implicitly. The reason why this happens is because Python has no concept of void.
🌐
Reddit
reddit.com › r/learnpython › eli5 how does "none" work exactly?
r/learnpython on Reddit: ELI5 how does "None" work exactly?
October 6, 2023 -

I get the general idea that None is a type when there is no specific value to be return.

But I am just confused on how to explicitly have a value of "None" . I am still learning python so I want to understand in which case does a function returns the value of "None". Like, it would also be helpful is someone could share a short function that displays "None" , preferably with no "return".

I've search online and there was an example of print(print("hi")), I kind of get this, the interpreter displays the first print and the second one is "None" because there is no value.

Thanks in advance

🌐
Julianhysi
julianhysi.com › post › 4
Julian's Blog - Bare return vs return None vs no return
This leaves the Python developer with a few options: - don't do anything (the function will implicitly return None) ... Now, all of these options will return None, so there's no difference in behavior. It's purely a choice of coding style, but that doesn't mean it's irrelevant. A good style will make the code more expressive and readable. Goes without saying that style is debatable at least to some extent, and there's no absolute right and wrong here.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-return-statement
Python return statement | DigitalOcean
August 3, 2022 - The python return statement is used to return values from the function. We can use the return statement in a function only. It can’t be used outside of a Python function. Every function in Python returns something. If the function doesn’t have any return statement, then it returns None. def print_something(s): print('Printing::', s) output = print_something('Hi') print(f'A function without return statement returns {output}') Output: Python Function Without Return Statement ·
🌐
Quora
quora.com › What-does-a-Python-function-return-if-no-return-is-given
What does a Python function return if no return is given? - Quora
print(no_return_func()) None [/code]Code explained: [code]def no_return_func(): pass [/code]Above is a ...
Find elsewhere
🌐
Finxter
blog.finxter.com › home › learn python blog › python return nothing/null/none/nan from function
Python Return Nothing/Null/None/NaN From Function - Be on the Right Side of Change
August 1, 2023 - In Python, a function returns None implicitly if there is no explicit return statement. However, you can also use the return statement followed by the keyword None to indicate that a function should return no value.
🌐
AskPython
askpython.com › home › python return statement
Python return Statement - AskPython
May 11, 2023 - In Python, every function returns something. If there are no return statements, then it returns None. If the return statement contains an expression, it’s evaluated first and then the value is returned.
🌐
Reddit
reddit.com › r/python › return none or not to return none
r/Python on Reddit: return None or not to return None
June 8, 2013 -

Several weeks ago a was pursuing some of the posts here on python and stumbled on an article about bad programming habits. I cant find the article again for reference but right at the top was a heady warning to avoid returning a meaningful None. AT the time I thought, 'well thats just droll and I never do that anyway', but as it turns out I do it all the time. If the function finds the correct thing then return the correct thing otherwise just end the function and python will return None for you.

So do you guys do the same thing? Whats wrong with returning None in the first place? What strategies do you use to avoid returning None?

🌐
Finxter
blog.finxter.com › python-function-return-none-without-return-statement
Python Function Return None Without Return Statement – Be on the Right Side of Change
If you don’t explicitly set a return value or you omit the return statement, Python will implicitly return the following default value: None.
🌐
Python.org
discuss.python.org › ideas
Deprecate bare return statements - Ideas - Discussions on Python.org
August 9, 2019 - FWIW “bare returns” (i.e. return vs return None ) I find are unintuitive as well. I think much of what is being said about bare except can also be said about “bare returns”. And while I feel PEP-8 does make an attempt to clarify this: “”" Be consistent in return statements.
🌐
EyeHunts
tutorial.eyehunts.com › home › python returns null function | example code
Python return null function | Example code
May 2, 2023 - If no explicit return statement is used, Python treats it as returning None · To literally return ‘nothing’ use pass, which basically returns the value None if put in a function(Functions must return a value, so why not ‘nothing’).
🌐
Codexpanse
codexpanse.com › courses › python-part-2 › Function-without-return.html
Function without return - Python, part 2: Functions
Unfortunately, it doesn't work like pow. Instead of 9 we see some weird None. It's a special value represented by the keyword None (see full list of keywords in Python, part 1: Statements).