3

I am a beginner programmer using python and I wonder what the best practice is for returns in a function. I have a lot of conditions, but as soon as one of them is true, I want to kill the function and return a bool value. say, we have:

a)
def foo():
    if condition1:
        return True
    if condition2:
        return True
    if condition3:
        return True
    return False

b)
def foo():
    bar = False
    if condition1:
        bar = True
    elif condition2:
        bar = True
    elif condition3:
        bar = True
    return bar

Is one way better than another? Why? Or is this some utter junk and should be implemented in a completly different way? Is it different in other languages than in python (or, is there a "pythonic" way)? Thank you all in advance for your answers. BTW, is there a tag for best practice, or something like that?

  • 2
    I doubt this is specific to Python, and there's a lot to find when searching for "early return". It's also very situation specific, such as how clear or how hidden the early returns are. And, in your case: `if condition1 or condition2 or condition3: return True; return False` might even clearer. –  Nov 08 '16 at 22:27
  • 1
    Both are fine. You should use whatever is more readable in your case. – zvone Nov 08 '16 at 22:28
  • For long functions it can be easier to read using a single return. But maybe breaking those longer functions up is a good idea anyway. – John La Rooy Nov 08 '16 at 22:40

3 Answers3

5

The first version is more Pythonic in my opinion, it could be written like this instead though:

return condition1 or condition2 or condition3
Francisco
  • 10,918
  • 6
  • 34
  • 45
  • 2
    If there's any possibility that any of the conditions aren't `bool`, you're better off casting the result: `return bool(condition1 or condition2 or condition3)`. – Mark Ransom Nov 08 '16 at 22:29
  • 2
    @MarkRansom `return any((condition1, condition2, condition3))` – John La Rooy Nov 08 '16 at 22:42
  • in this case, condition1 will check. If true, will it immediatly be return True, or do the other conditions need check anyways? I believe C has something like fast evaluation, do we have it here? – Jaro Stamov Nov 09 '16 at 09:39
  • @JaroStamov yes, there is. You can even use it as some kind of `None` check, for instance: `return item and item.property or default` – Назар Топольський Nov 09 '16 at 10:06
3

Both a.) and b.) are valid. For languages with resource management like C, or debugging and wanting to break in only one place, the preference is b.). See https://softwareengineering.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from. For Python and other languages it is standard to use either. See Should a function have only one return statement?

Community
  • 1
  • 1
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
  • Can you elaborate on this for me; is there a way to see this? It returned at `return`, how would I see which `if/elif` case caused `bar = True`? – roganjosh Nov 08 '16 at 22:30
  • 3
    @roganjosh It doesn't tell you which condition was true. But if you want to set a breakpoint or add a print statement before the function returns, you only have to put it in one place. – Barmar Nov 08 '16 at 22:31
  • @Barmar Actually, I see that point. Fair, thanks. – roganjosh Nov 08 '16 at 22:33
2

You don't use the "bar" variable and you don't need to create it.

Other ways:

def f():
    return condition1 or condition2 or condition3

def f2():
    return any([condition1, condition2, condition3])
ADR
  • 1,255
  • 9
  • 20