Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to tell the for loop to continue from a function?

Tags:

Sometimes I need the following pattern within a for loop. At times more than once in the same loop:

try:     # attempt to do something that may diversely fail except Exception as e:     logging.error(e)     continue 

Now I don't see a nice way to wrap this in a function as it can not return continue:

def attempt(x):     try:         raise random.choice((ValueError, IndexError, TypeError))     except Exception as e:         logging.error(e)         # continue  # syntax error: continue not properly in loop         # return continue  # invalid syntax         return None  # this sort of works 

If I return None than I could:

a = attempt('to do something that may diversely fail') if not a:     continue 

But I don't feel that does it the justice. I want to tell the for loop to continue (or fake it) from within attempt function.

like image 639
fmalina Avatar asked May 20 '11 11:05

fmalina


People also ask

Can you return continue from a function?

To leave a function u can simply use return. Big boss, break and continue are used in loops and statements. if you include any loop in your function, then you can use either break or continue, else use return.

How do you use continue in a for loop?

The continue statement is used inside a for loop or a while loop. The continue statement skips the current iteration and starts the next one. Typically, you use the continue statement with an if statement to skip the current iteration once a condition is True .

Can you use continue in a function Python?

A function cannot cause a break or continue in the code from which it is called. The break/continue has to appear literally inside the loop.

How does continue work in Python for loop?

The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops.


2 Answers

Python already has a very nice construct for doing just this and it doesn't use continue:

for i in range(10):     try:         r = 1.0 / (i % 2)     except Exception, e:         print(e)     else:         print(r) 

I wouldn't nest any more than this, though, or your code will soon get very ugly.

In your case I would probably do something more like this as it is far easier to unit test the individual functions and flat is better than nested:

#!/usr/bin/env python  def something_that_may_raise(i):     return 1.0 / (i % 2)  def handle(e):     print("Exception: " + str(e))  def do_something_with(result):     print("No exception: " + str(result))  def wrap_process(i):     try:         result = something_that_may_raise(i)     except ZeroDivisionError, e:         handle(e)     except OverflowError, e:         handle(e) # Realistically, this will be a different handler...     else:         do_something_with(result)  for i in range(10):     wrap_process(i) 

Remember to always catch specific exceptions. If you were not expecting a specific exception to be thrown, it is probably not safe to continue with your processing loop.

Edit following comments:

If you really don't want to handle the exceptions, which I still think is a bad idea, then catch all exceptions (except:) and instead of handle(e), just pass. At this point wrap_process() will end, skipping the else:-block where the real work is done, and you'll go to the next iteration of your for-loop.

Bear in mind, Errors should never pass silently.

like image 181
Johnsyweb Avatar answered Jan 15 '23 03:01

Johnsyweb


The whole idea of exceptions is that they work across multiple levels of indirection, i.e., if you have an error (or any other exceptional state) deep inside your call hierarchy, you can still catch it on a higher level and handle it properly.

In your case, say you have a function attempt() which calls the functions attempt2() and attempt3() down the call hierarchy, and attempt3() may encounter an exceptional state which should cause the main loop to terminate:

class JustContinueException(Exception):     pass  for i in range(0,99):     try:         var = attempt() # calls attempt2() and attempt3() in turn     except JustContinueException:         continue # we don't need to log anything here     except Exception, e:         log(e)         continue      foo(bar)  def attempt3():     try:         # do something     except Exception, e:         # do something with e, if needed         raise # reraise exception, so we catch it downstream 

You can even throw a dummy exception yourself, that would just cause the loop to terminate, and wouldn't even be logged.

def attempt3():     raise JustContinueException() 
like image 21
Boaz Yaniv Avatar answered Jan 15 '23 04:01

Boaz Yaniv