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

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

问题描述

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

我想知道Python中是否存在这样的循环命名系统。我多次遇到了这样的情况:我需要从内部循环中的外部循环。通常,我通过将内部循环放入一个函数中来解决这个问题,该函数返回一个布尔值作为中断条件。但标签循环打破似乎更简单,我想尝试,如果这样的功能存在于Python中



有谁知道它是否?

解决方案

有人建议在Python中包含命名循环这里的解释拒绝了。拒绝主要是由于罕见的情况下代码的可读性将通过包括这个结构改善。

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?

解决方案

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天全站免登陆