2

Is there any practical differences between using:

def some_function():
    print('Hello!')
    return()

and:

def some_function():
    print('Hello!')

I know that return isn't required, but is it bad pratice to not return after a function has been called?

1
  • 2
    Python functions always return: when return statement is missing, None will be returned implicitly. In your first example you're returning a tuple, that's probably is not what you want. Nov 13, 2012 at 10:49

4 Answers 4

6

First of all, return is not a function; it is a statement. There is no need to add the parenthesis.

Functions in python without a return statement return None by default. An empty return statement does the same, so there is no difference.

>>> def foo(): return
...
>>> foo()
None
>>> def bar(): pass
...
>>> bar()
None
1

If you don't use return statement, your function returns None. It is not a bad practice.

0

There is no difference. Both functions return None, the latter does is implicitly the former explicitly.

1
  • 1
    The zen of python (import this): Explicit is better than implicit. Nov 13, 2012 at 11:10
0

A bare return statement is useful in one situation: when you want your function to stop running before it would otherwise do so. For example:

def loop_example():
    for some_item in some_sequence:
        # do something
        if some_condition:
            # no need to continue for some reason
            return

If that's not the case, it's just visual clutter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.