4

What are the benefits of writing:

def contains(lst, n):
  for x in lst:
    if x == n:
      return True
  return False

instead of:

def contains(lst, n):
  for x in lst:
    if x == n:
      return True

?

Please note that in the second example the last line of code is missing, and the function returns None instead of False.

Side note: I'm aware I should write n in lst, this is just an example.

Nick
  • 10,309
  • 21
  • 97
  • 201
  • It's worth noting that in most such real-life examples, where you can't just `return n in lst`, you can still `return any(n *** x for x in lst)`, where `n *** x` is whatever expression you need to do. – abarnert Nov 05 '14 at 21:34

3 Answers3

6

I think to some extent this is a matter of personal preference.

I personally would always prefer former over the latter, since "explicit is better than implicit".

Also, if the function is expected to return a bool, I think it's cleaner to return a bool and not some other object whose truthiness needs to be evaluated in order to get a bool.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

It really depends on how you intend the function to be used.

If it's only ever going to be used in a boolean context, it might make sense to define and document it as "returns something truthy or something falsey", in which case it's reasonable to return None as something falsey.

But if you expect people to, say, log the result for human debugging purposes, or use it as a key in a dictionary—or you just don't know how it's going to be used—it's a lot clearer to say it returns a boolean and then explicitly return False.

In fact, it's often a nice idea to be stricter than you promise in what you provide; even if you only document that you're returning something truthy or falsey, it won't actually hurt to always return True or False, unless you have some other value that could be meaningful.* As the other answers imply, this doesn't just go along with general programming principles, but also the specific Python Zen "explicit is better than implicit".

* If you're thinking that it could hurt performance, it won't, even if that were an issue. An implicit return None complies to exactly the same LOAD_CONST and RETURN_VALUE as an explicit return None—or, of course, an explicit return False.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 1
    It also helps to make your intention clear to the reader of the code. A missing return statement might make the reader ask themselves, "Did they author forget to return something? Should I put in a return statement here to fix the code?" and so on. Minimizing such concerns makes the code much easier to read. – kqr Nov 07 '14 at 11:32
  • @kqr: Good point. Implementation may not be as important as interface, but it's still important—especially in Python, where people may be apt to say "the code is the documentation" than in less-readable languages… – abarnert Nov 07 '14 at 19:49
3

If you don't explicitly return anything, the return value will be None. If the function should return a boolean value, you really should explicitly return False.

In your case, None will evaluate to False when implicitly converted to bool, but I wouldn't depend on such behavior. Basically just to be explicit and to improve readability.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218