Consider three functions:
def my_func1():
print "Hello World"
return None
def my_func2():
print "Hello World"
return
def my_func3():
print "Hello World"
They all appear to return None. Are there any differences between how the returned value of these functions behave? Are there any reasons to prefer one versus the other?
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 valueNone
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
'smother
if theperson
given is a human. If it's not a human, we returnNone
since theperson
doesn't have amother
(let's suppose it's not an animal or so).Note: You should never do
var = get_mother()
, since let us assume var is a dictionary and you iterate by var.items() it will give an error citing None has no method items(). It you intentionally return None then it must be taken care of in the calling function appropriately.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 tho you don't need it that often.We got 15
prisoners
and we know one of them has a knife. We loop through eachprisoner
one by one to check if they have a knife. If we hit the person with a knife, we can just exit the function cause we know there's only one knife and no reason the check rest of theprisoners
. If we don't find theprisoner
with a knife, we raise an the alert. This could be done in many different ways and usingreturn
is probably not even the best way, but it's just an example to show how to usereturn
for exiting a function.Note: You should never do
var = find_prisoner_with_knife()
, since the return value is not meant to be caught.Using no
return
at allThis 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 asreturn
invoid
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.
Note: You should never do
var = set_mother(my_person, my_mother)
, since the return value is not meant to be caught.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.
They each return the same singleton
None
-- There is no functional difference.I think that it is reasonably idiomatic to leave off the
return
statement unless you need it to break out of the function early (in which case a barereturn
is more common), or return something other thanNone
. It also makes sense and seems to be idiomatic to writereturn None
when it is in a function that has another path that returns something other thanNone
. Writingreturn None
out explicitly is a visual cue to the reader that there's another branch which returns something more interesting (and that calling code will probably need to handle both types of return values).Often in Python, functions which return
None
are used likevoid
functions in C -- Their purpose is generally to operate on the input arguments in place (unless you're using global data (shudders)). ReturningNone
usually makes it more explicit that the arguments were mutated. This makes it a little more clear why it makes sense to leave off thereturn
statement from a "language conventions" standpoint.That said, if you're working in a code base that already has pre-set conventions around these things, I'd definitely follow suit to help the code base stay uniform...
In terms of functionality these are all the same, the difference between them is in code readability and style (which is important to consider)