2

I'm viewing the source code of a project now. In which I saw a function like this.

def func(x):
    if condition_a:
        return
    if condition_b:
        return
    process_something

What does this return with nothing do here?

Zen
  • 4,381
  • 5
  • 29
  • 56

1 Answers1

6

The "return with nothing" exits the function at that line and returns None. As Python's docs say:

If an expression list is present, it is evaluated, else None is substituted.

return leaves the current function call with the expression list (or None) as return value.

In flow control, I've seen it commonly used when some sort of condition is encountered that makes it impossible to execute the rest of the function; for example,

def getDataFromServer():
    if serverNotResponding():
        return
    # do important stuff here which requires the server to be running
Community
  • 1
  • 1
APerson
  • 8,140
  • 8
  • 35
  • 49