245

Let's assume an iteration in which we call a function without a return value. The way I think my program should behave is explained in this pseudocode:

for element in some_list:
    foo(element)

def foo(element):
    do something
    if check is true:
        do more (because check was succesful)
    else:
        return None
    do much much more...

If I implement this in python, it bothers me, that the function returns a None. Is there a better way for "exiting a function, that has no return value, if a check fails in the body of the function"?

Robin
  • 605
  • 2
  • 8
  • 25
Aufwind
  • 25,310
  • 38
  • 109
  • 154
  • 8
    Python always returns None if you don't explicitly return something. But you can leave the None off. – Keith May 31 '11 at 16:47
  • 3
    Depending on what the check is, you might also `raise` an exception (or, very rarely, make the function return True/False) – Rosh Oxymoron May 31 '11 at 16:47

4 Answers4

408

You could simply use

return

which does exactly the same as

return None

Your function will also return None if execution reaches the end of the function body without hitting a return statement. Returning nothing is the same as returning None in Python.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • `return` doesn't work, if i set `a = method()` , inside method i use `return`, it still keep running code behind a. exit should be like php exit(), it breaks the program instantly. – TomSawyer Sep 24 '17 at 18:29
  • 3
    @TomSawyer to stop a Python program early, do `import sys` first and then [`sys.exit()`](https://docs.python.org/library/sys.html#sys.exit) if you want to exit but report success or `sys.exit("some error message to print to stderr")`. – Boris Verkhovskiy Mar 04 '20 at 17:07
  • @Boris, this is what I was looking for and this worked for me. – mikey Mar 05 '20 at 20:43
  • 6
    @TomSawyer what you ask for is the topic of a [different question](https://stackoverflow.com/questions/73663/how-to-terminate-a-python-script)! Ordinary `return` works perfectly well for what _this_ OP described. – alexis Sep 16 '20 at 05:53
21

I would suggest:

def foo(element):
    do something
    if not check: return
    do more (because check was succesful)
    do much much more...
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
17

you can use the return statement without any parameter to exit a function

def foo(element):
    do something
    if check is true:
        do more (because check was succesful)
    else:
        return
    do much much more...

or raise an exception if you want to be informed of the problem

def foo(element):
    do something
    if check is true:
        do more (because check was succesful)
    else:
        raise Exception("cause of the problem")
    do much much more...
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
9
  1. return None or return can be used to exit out of a function or program, both does the same thing
  2. quit() function can be used, although use of this function is discouraged for making real world applications and should be used only in interpreter.
    import site
    
    def func():
        print("Hi")
        quit()
        print("Bye")
  1. exit() function can be used, similar to quit() but the use is discouraged for making real world applications.
import site
    
    def func():
        print("Hi")
        exit()
        print("Bye")
  1. sys.exit([arg]) function can be used and need to import sys module for that, this function can be used for real world applications unlike the other two functions.
import sys 
  height = 150
  
if height < 165: # in cm 
      
    # exits the program 
    sys.exit("Height less than 165")     
else: 
    print("You ride the rollercoaster.") 
  1. os._exit(n) function can be used to exit from a process, and need to import os module for that.
Saptarshi das
  • 379
  • 3
  • 6