Why would a function end with "return 0" instead of "return" in python?

Question:

Could you please explain the difference between “return 0” and “return”?
For example:

do_1():
    for i in xrange(5):
        do_sth()
    return 0

do_2():
    for i in xrange(5):
        do_sth()
    return 

What is the difference between two functions above?

Asked By: alwbtc

||

Answers:

def do_1():
    return 0

def do_2():
    return

# This is the difference
do_1 == 0 # => True
do_2 == 0 # => False
Answered By: user647772

it has nothing to do specifically with python.

whenever you perform a function you can optionally return a value.

the return keyword is what informs the function if it should return a value or not.

if no value is given to the return or no variable is assigned to be returned, then the return value is None

if you assign a value, in this case, 0 to be returned, then the value 0 will be returned by the function and the function will end when the return keyword and value is reached.

some more information on 0:
the reason a 0 would be used is because it is commonplace that functions which return 0 were “successful” and non-zero return values are either simply the value to be returned, or sometimes error codes if the function did not perform correctly.

Answered By: Inbar Rose

In python, a function would return None either explicitly or implicitly.

e.g.

# Explicit
def get_user(id):
    user = None
    try:
        user = get_user_from_some_rdbms_byId(id)
    except:
        # Our RDBMS raised an exception because the ID was not found.
        pass
    return user  # If it is None, the caller knows the id was not found.

# Implicit
def add_user_to_list(user):
    user_list.append(user)   # We don't return something, so implicitly we return None

A python function would return 0 either because of some computation:

def add_2_numbers(a,b):
    return a + b      # 1 -1 would return 0

Or because of a magic flag kind of thing, which is frowned upon.

But in python we don’t use 0 to denote success because this:

if get_user(id):

would not evaluate to True if we returned 0 therefore this if branch would not run.

In [2]: bool(0)
Out[2]: False
Answered By: rantanplan

Depends on usage:

>>> def ret_Nothing():
...     return
... 
>>> def ret_None():
...     return None
... 
>>> def ret_0():
...     return 0
... 
>>> ret_Nothing() == None
True
>>> ret_Nothing() is None  # correct way to compare values with None
True
>>> ret_None() is None
True
>>> ret_0() is None
False
>>> ret_0() == 0
True
>>> # and...
>>> repr(ret_Nothing())
'None'

And as mentioned by Tichodroma, 0 is not equal to None. However, in boolean context, they are both False:

>>> if ret_0():
...     print 'this will not be printed'
... else:
...     print '0 is boolean False'
... 
0 is boolean False
>>> if ret_None():
...     print 'this will not be printed'
... else:
...     print 'None is also boolean False'
... 
None is also boolean False

More on Boolean context in Python: Truth Value Testing

Answered By: aneroid

In Python, every function returns a return value, either implicitly or explicitly.

>>> def foo():
...     x = 42
... 
>>> def bar():
...     return
... 
>>> def qux():
...     return None
... 
>>> def zero():
...     return 0
... 
>>> print foo()
None
>>> print bar()
None
>>> print qux()
None
>>> print zero()
0

As you can see, foo, bar and qux return exactly the same, the built in constant None.

  • foo returns None because a return statement is missing and None is the default return value if a function doesn’t explicitly return a value.

  • bar returns None because it uses a return statement without an argument, which also defaults to None.

  • qux returns None because it explicitly does so.

zero however is entirely different and returns the integer 0.

If evaluated as booleans, 0 and None both evaluate to False, but besides that, they are very different (different types in fact, NoneType and int).

Answered By: Lukas Graf
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.