Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redo for loop iteration in Python

Does Python have anything in the fashion of a "redo" statement that exists in some languages?

(The "redo" statement is a statement that (just like "break" or "continue") affects looping behaviour - it jumps at the beginning of innermost loop and starts executing it again.)

like image 936
Matjaž Leonardis Avatar asked Apr 12 '16 12:04

Matjaž Leonardis


People also ask

How do you redo an iteration in Python?

(The "redo" statement is a statement that (just like "break" or "continue") affects looping behaviour - it jumps at the beginning of innermost loop and starts executing it again.)

Is there a repeat loop in Python?

Overview. Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; others repeat statements until a condition is True. There are also loops that repeat statements a specific number of times.

How do you restart a function in Python?

Calling the start() function on a terminated process will result in an AssertionError indicating that the process can only be started once. Instead, to restart a process in Python, you must create a new instance of the process with the same configuration and then call the start() function.


2 Answers

No, Python doesn't have direct support for redo. One option would something faintly terrible involving nested loops like:

for x in mylist:
    while True:
        ...
        if shouldredo:
            continue  # continue becomes equivalent to redo
        ...
        if shouldcontinue:
            break     # break now equivalent to continue on outer "real" loop
        ...
        break  # Terminate inner loop any time we don't redo

but this mean that breaking the outer loop is impossible within the "redo-able" block without resorting to exceptions, flag variables, or packaging the whole thing up as a function.

Alternatively, you use a straight while loop that replicates what for loops do for you, explicitly creating and advancing the iterator. It has its own issues (continue is effectively redo by default, you have to explicitly advance the iterator for a "real" continue), but they're not terrible (as long as you comment uses of continue to make it clear you intend redo vs. continue, to avoid confusing maintainers). To allow redo and the other loop operations, you'd do something like:

# Create guaranteed unique sentinel (can't use None since iterator might produce None)
sentinel = object()
iterobj = iter(mylist)  # Explicitly get iterator from iterable (for does this implicitly)
x = next(iterobj, sentinel)  # Get next object or sentinel
while x is not sentinel:     # Keep going until we exhaust iterator
    ...
    if shouldredo:
        continue
    ...
    if shouldcontinue:
        x = next(iterobj, sentinel)  # Explicitly advance loop for continue case
        continue
    ...
    if shouldbreak:
        break
    ...
    # Advance loop
    x = next(iterobj, sentinel)

The above could also be done with a try/except StopIteration: instead of two-arg next with a sentinel, but wrapping the whole loop with it risks other sources of StopIteration being caught, and doing it at a limited scope properly for both inner and outer next calls would be extremely ugly (much worse than the sentinel based approach).

like image 100
ShadowRanger Avatar answered Sep 21 '22 16:09

ShadowRanger


No, it doesn't. I would suggest using a while loop and resetting your check variable to the initial value.

count = 0
reset = 0
while count < 9:
   print 'The count is:', count
   if not someResetCondition:
       count = count + 1
like image 29
cmaynard Avatar answered Sep 21 '22 16:09

cmaynard