weidler
weidler

Reputation: 676

Python: If Condition then skip to return

I wondered if there is a nice way to tell the python interpreter to skip to the next/last return statement of a function.

Lets assume the following dummy code:

def foo(bar):
  do(stuff)

  if condition:
    do(stuff)
    if condition2:
      do(stuff)
      if condition3:
        ...

  return (...)

Sometimes this gets really messy with many conditions that i cant chain because they rely on on the block do(stuff) above. I could now do this:

def foo(bar):
  do(stuff)

  if not condition: return (...)
  do(stuff)
  if not condition2: return (...)
  do(stuff)
  if not condition3: return (...)
    ...

  return (...)

It looks a little less messy but i would have to repeat the return statement again and again which is somwhat anoying and if its a long tuple or similar it even looks worse. The perfect solution would be to say "if not condition, skip to the final return statement". Is this somehow possible?

edit: to make this clear: my goal is to improve readability while avoiding a drop in performance

Upvotes: 4

Views: 21569

Answers (3)

Branko
Branko

Reputation: 69

I would suggest this:

def foo(bar):

  for __ in [0]:
     do(stuff)

     if not condition: continue
     do(stuff)
     if not condition2: continue
     do(stuff)
     if not condition3: continue
     ...

  return (...)

Or possibly:

def foo(bar):

  while True:
     do(stuff)

     if not condition: break
     do(stuff)
     if not condition2: break
     do(stuff)
     if not condition3: break
     ...
     break
  return (...)

It's a bit cleaner and avoids having multiple returns :+1: From the two the first one may be better as it explicitly shows that there is no intention on looping.

Upvotes: 0

chepner
chepner

Reputation: 532448

Refactoring your code is a much better idea than what I am about to suggest, but this is an option.

class GotoEnd(Exception):
    pass

def foo(bar):

  try:
    do(stuff)
    if not condition: raise GotoEnd
    do(stuff)
    if not condition2: raise GotoEnd

    do(stuff)
    if not condition3: raise GotoEnd

    ...
  except GotoEnd:
    pass

  return (...)

Upvotes: -1

DeepSpace
DeepSpace

Reputation: 81684

I think I would create a list of functions (I assume that all the do(stuff) in your example are actually different functions). Then you can use a for loop:

list_of_funcs = [func1, func2, func3]
for func in list_of_funcs:
    func(stuff)
    if not condition:
        break
return (...)

If the conditions are different then you can also create a list of conditions (which will be a list of functions that return True or False), then you can use zip in the following manner:

list_of_funcs = [func1, func2, func3]
list_of_conditions = [cond1, cond2, cond3]
for func, cond in zip(list_of_funcs, list_of_conditions):
    func(stuff)
    if not cond():
        break
return (...)

This way your actual code stays the same length and in the same indentation level, no matter how many functions and conditions you may have.

Upvotes: 5

Related Questions