0

I need to break out of a for loop according to the result obtained after calling a function. This is an example of what I'm after (does not work obviously):

def break_out(i):
    # Some condition
    if i > 10:
        # This does not work.
        return break

for i in range(1000):
    # Call function
    break_out(i)

Of course this is a very simple MWE, my actual function is much bigger which is why I move it outside of the for loop.

This answer says it is not possible and I should make the function return a boolean and add an if statement inside the for loop to decide.

Since it's a rather old question and it didn't get much attention (also, it's applied to while loops), I'd like to re-check if something like this is possible.

5
  • 1
    Why is the linked answer not suitable? Commented Apr 15, 2015 at 13:44
  • @SiHa I didn't say it is not suitable, I am asking if there's a more direct way of doing it (as opposed to returning a flag and adding an if block). Commented Apr 15, 2015 at 13:45
  • 1
    Nope, no new way of doing it - previous question still applies Commented Apr 15, 2015 at 13:54
  • @Gabriel We're talking about a difference of one line here. And, to be honest, your example (if it did work) would be harder to debug because it's not obvious within the loop that something might exit it early. Commented Apr 15, 2015 at 14:04
  • @SiHa I know it's one line (or very few) but my question aimed at learning new possibilities rather than saving a handful of lines. Your point on debugging has value, perhaps add an answer giving this as a reason why it is not (or should not be) possible to do what I asked? Commented Apr 15, 2015 at 14:06

2 Answers 2

6

No, it's not (reasonably) possible. You could raise an exception, I suppose, but then you'll have to catch it with a try/except statement outside the loop.

Since OP has expressed curiosity about this, I'm going to explain why Python doesn't allow you to do this. Functions are supposed to be composable elements. They're meant to work in different contexts.

Allowing a function to break out of a loop has two separate problems:

  1. Client code might not expect this behavior.
  2. What if there is no loop?

For (1), if I read some code like this:

import foo  # some library I didn't write

while condition:
    foo.bar()
assert not condition

I reasonably assume the assertion will not fire. But if foo.bar() was able to break out of the loop, it could.

For (2), perhaps I wrote this somewhere else:

if condition:
    foo.bar()

It's not clear what that should do if foo.bar() tries to break out of the loop, since there is no loop.

Sign up to request clarification or add additional context in comments.

3 Comments

Why not add a try/except inside the loop? And then put a break in the except?
@logic: An if is less typing.
True, raising errors is a bad method.
1

Rather than return break, return a value that forces a break condition. To extend your example:

def break_out(i):
    # Some condition
    return i > 10  # Returns `True` if you should break from loop.


for i in range(1000):
    # Call function
    if break_out(i):
        break

    # Insert the rest of your code here...

3 Comments

Yep, this is what the OP mentions in his question and also asks if it can be done differently.
Yep, that's what I was trying to avoid (also it's pretty much exactly the answer in the question linked) Thanks anyway!
Ack! Egg on my face. This answer popped into my head so quickly that I skimmed the rest of the question. Sorry!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.