T. Christiansen
T. Christiansen

Reputation: 1038

Most pythonic way of function with no return?

A I couldn't find anything concerning in the PEP 8. I'm interested in your thoughts about the most pythonic syntax of function which have no return?

Are there any reason to prevent functions without a return line(example 3)?

Example 1:

def foo():
   print 'foo'
   return None

Example 2:

def foo():
   print 'foo'
   pass

Example 3:

def foo():
   print 'foo'

Upvotes: 13

Views: 8163

Answers (5)

Vasili Syrakis
Vasili Syrakis

Reputation: 9601

The PEP8 has a recommendation on this topic:

Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should. If any return statement returns an expression, any return statements where no value is returned should explicitly state this as return None , and an explicit return statement should be present at the end of the function (if reachable).

Yes:

def foo(x):
    if x >= 0:
        return math.sqrt(x)
    else:
        return None

def bar(x):
    if x < 0:
        return None
    return math.sqrt(x)

No:

def foo(x):
    if x >= 0:
        return math.sqrt(x)

def bar(x):
    if x < 0:
        return
    return math.sqrt(x)

Upvotes: 11

Adam Gent
Adam Gent

Reputation: 49085

As others have stated Option 3 is by far the best.

Even more of a reason Option 3 is a good idea is that many people are of the opinion that None (aka null in other languages) should generally never be returned and that you should throw an exception or use the Null Object pattern. While I don't entirely agree with that I think there is some valid point of avoiding using None particularly explicitly when it doesn't semantically make sense and is ambiguous.

I'm not saying you should never return None but in general I don't think its a good idea.

You can even see this in Python today where blah['DOESNOTEXIST'] will throw an exception where in Java map.get("DOESNOTEXIST") will return null.

Upvotes: -1

madjar
madjar

Reputation: 12951

If you function returns a value, that may be None is some case, return None is good.

If you function doesn't return a value, but you want to return early, use return without any parameter. On the last line, you can omit it.

pass does nothing, and is only useful when a statement is needed to avoir a syntax error (in an empty class, for example)

Upvotes: 7

georg
georg

Reputation: 214959

There are two use cases:

  • a function that never returns anything (a.k.a "procedure"). You should omit return altogether.
  • a function that returns None for some inputs. I personally prefer to put return None there, because otherwise it might slightly confuse the reader:

example:

 def find_user(name):
     if database.is_ready():
        return database.find_user_by_name(name)
     else:
        return None

Technically, you can drop the else part, but that would make the function look confusing.

Upvotes: 15

b3orn
b3orn

Reputation: 322

Keep it simple. Example 3 is the most pythonic way.

>>> import this

The Zen of Python, by Tim Peters

...
Simple is better than complex.
...

Upvotes: 18

Related Questions