Python 中的命名循环 [英] Naming Loops in Python

查看:36
本文介绍了Python 中的命名循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了这个问题一个关于在 Java 中标记循环的解决方案.

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

我想知道 Python 中是否存在这样的循环命名系统.我多次遇到过需要从内部 for 循环跳出外部 for 循环的情况.通常,我通过将内部循环放在一个函数中来解决这个问题,该函数返回(除其他外)一个用作中断条件的布尔值.但是标记循环中断似乎要简单得多,如果 python 中存在这样的功能,我想尝试一下

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

有人知道吗?

推荐答案

有人提议在 python 中包含命名循环 PEP3136,然而,它被拒绝并解释这里.拒绝的主要原因是在极少数情况下,通过包含此构造可以提高代码可读性.

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.

这篇关于Python 中的命名循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆