Naming Loops in Python

不问归期 提交于 2019-11-26 17:11:42

问题


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?


回答1:


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.




回答2:


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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/8419796/naming-loops-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!