30

I recently read this question which had a solution about labeling loops in Java.

I am wondering if such a loop-naming system exists in Python. I have been in a situation multiple times where I do need to break out of an outer for loop from an inner for loop. Usually, I solve this problem by putting the inner loop in a function that returns (among others) a boolean which is used as a breaking condition. But labeling loops for breaking seems a lot simpler and I would like to try that, if such functionality exists in python.

Does anyone know if it does?

5
  • Check the second answer, hopefully it helps some. But from what I can see, no such system exists. Commented Dec 7, 2011 at 17:37
  • "But labeling loops for breaking seems a lot simpler "? Simpler than proper functions? How so? Can you provide some evidence of how this would be "simpler"? Commented Dec 7, 2011 at 18:38
  • 2
    I don't have an example handy, but it would be a situation where I would have to create a function which will be used only in that one spot. Perhaps "simpler" was the wrong word. What I meant was that I wouldn't have to define a new function just for use in that ONE spot. Commented Dec 7, 2011 at 18:46
  • What's wrong with nested function definitions? Also, what about redesigning the inner loop to avoid the break? Commented Dec 7, 2011 at 19:01
  • 2
    I just don't like them. I guess it's a personal preference Commented Dec 7, 2011 at 20:43

4 Answers 4

21

There was a proposal to include named loops in python PEP3136, however, it was rejected with an explanation here. The rejection was mostly due to the rare number of circumstances where code readability would be improved by including this construct.

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

1 Comment

Labeled loops are often used as an alternative to goto in other languages. Python doesn't have goto, but there are several alternatives.
20

Here's a way to break out of multiple, nested blocks using a context manager:

import contextlib

@contextlib.contextmanager
def escapable():
    class Escape(RuntimeError): pass
    class Unblock(object):
        def escape(self):
            raise Escape()

    try:
        yield Unblock()
    except Escape:
        pass

You can use it to break out of multiple loops:

with escapable() as a:
    for i in xrange(30):
        for j in xrange(30):
            if i * j > 6:
                a.escape()

And you can even nest them:

with escapable() as a:
    for i in xrange(30):
        with escapable() as b:
            for j in xrange(30):
                if i * j == 12:
                    b.escape()  # Break partway out
                if i * j == 40:
                    a.escape()  # Break all the way out

2 Comments

yeah, that is extremely more readable than simply doing a loop with; break 2 (php) or continue loop1; (javascript).. also, how pythonic is this indent hell spaghetti code really? Using raise StopIteration seems to be the cleanest, most readable, pythonic solution
No, it's not. It's better to add another explicit variable to control if outer loop should continue. Any method should be clear and should not require understanding of very-specific (outside business knowledge) structures. Especially context managers!
15

Though there are reasons to include named looped in language construct you can easily avoid it in python without loss of readability. An implementation of the referred example in python

>>> try:
    for i in xrange(0,5):
        for j in xrange(0,6):
            if i*j > 6:
                print "Breaking"
                raise StopIteration
            print i," ",j
except StopIteration:
    print "Done"


0   0
0   1
0   2
0   3
0   4
0   5
1   0
1   1
1   2
1   3
1   4
1   5
2   0
2   1
2   2
2   3
Breaking
Done
>>> 

I solve this problem by putting the inner loop in a function that returns (among others) a boolean which is used as a breaking condition.

I think you should try this. This is very pythonic, simple and readable.

3 Comments

+1. Using exceptions for flow control is perfectly cromulent in Python, though it is considered bad style in other languages, usually for performance reasons.
What if I just want to "continue" the outer loop, rather than stopping it?
@Guillochon Just put the try statement inside the first for loop
2

Nope.

Depending on what you are doing, there is good chance you can use something from itertools to flatten your nested for loops into a single for loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.