break and continue in function
Asked Answered
E

5

24
def funcA(i):
   if i%3==0:
      print "Oh! No!",
      print i
      break

for i in range(100):
   funcA(i)
   print "Pass",
   print i

I know script above won't work. So, how can I write if I need put a function with break or continue into a loop?

Embroidery answered 21/12, 2012 at 8:48 Comment(0)
P
35

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. Your options are:

  1. return a value from funcA and use it to decide whether to break
  2. raise an exception in funcA and catch it in the calling code (or somewhere higher up the call chain)
  3. write a generator that encapsulates the break logic and iterate over that instead over the range

By #3 I mean something like this:

def gen(base):
    for item in base:
        if item%3 == 0:
           break
        yield i

for i in gen(range(1, 100)):
    print "Pass," i

This allows you to put the break with the condition by grouping them into a generator based on the "base" iterator (in this case a range). You then iterate over this generator instead of over the range itself and you get the breaking behavior.

Petiolule answered 21/12, 2012 at 8:52 Comment(3)
a generator that encapsulates the break logic?Embroidery
@Embroidery - something like itertools.takewhile probablyJanice
@ThunderEX: Yes, similar to takewhile. See my edited answer.Petiolule
S
3

Elaborating BrenBarns answer: break fortunately will not propagate. break is to break the current loop, period. If you want to propagate an event, then you should raise an exception. Although, raising the exception to break the loop is a really ugly way to break loops and a nice way to break your code.

KISS! The simplest would be to check the condition directly in the loop

def my_condition(x):
  return x == 4

for i in xrange(100):
  if my_condition(i): break
  print i

If, for some reason, you want to propagate an exception, then you use it like this

# exception example
for i in xrange(100):
  if i == 4: raise Exception("Die!")
  print i

As mentioned, it is a really ugly design. Imagine you forget to catch this exception, or you change its type from Exception to MyBreakException and forget to change it somewhere in try/except higher part of the code...

The generator example has its merits, it makes your code more functional style (which I presonally adore)

# generator example
def conditional_generator(n, condition):
  for i in xrange(n):
    if condition(i):
      break
    else:
      yield i


for i in conditional_generator( 100, my_condition ):
  print i

...which is similar to takewhile, mentioned by eumiro

Sporades answered 21/12, 2012 at 9:18 Comment(0)
J
1
def funcA(i):
   if i%3==0:
      print "Oh! No!",
      print i
      return True
   else:
      return False

for i in range(100):
   if funcA(i):
       break
   print "Pass",
   print i
Janice answered 21/12, 2012 at 8:51 Comment(1)
Could you add an explanation of how this code answers the question?Semipro
S
1

Break won't propagate between functions, you need to put it directly within the loop somewhere.

Sherri answered 21/12, 2012 at 8:51 Comment(0)
C
0

Regarding the related question Python Break Inside Function, another possible work around his "fantasy code":

def stopIfZero(a):
    if int(a) == 0:
        break
    else:
        print('Continue')

while True:
    stopIfZero(input('Number: '))

would be this:

def stopIfZero(a):
    if int(a) == 0:
        return False
    else:
        print('Continue')
        return True

while stopIfZero(input('Number: ')):
    continue
Carnify answered 7/5 at 22:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.