[Python-ideas] Loop labels

Matt Joiner anacrolix at gmail.com
Fri Mar 9 12:08:05 CET 2012

> Labels are neither names nor keywords nor objects. They would be
> instructions to the compiler, nothing more.


> The idea of labelled break/continue is not a new invention. Before
> criticising it (or for that matter, praising it), we should see how it
> works in other languages.

It's also in C (in the form of goto), some shells.

> For what it's worth, I used to be against this idea as adding
> unnecessary complexity, until a year or so ago when I came across a
> use-case which was naturally written as nested loops with labelled
> breaks. Now I'm sold on the idea. I ended up re-writing the code to use
> functions, but it really wasn't natural. It felt forced.

It's something that comes up enough to be of use. I'd compare it to
the with statement, which isn't always useful, but handles the
majority of such cases. A goto is the full fledged try/finally

Just as the with statement prevents people from incorrectly
implementing their try/finally's for common cases, Labelled
break/continues would prevent people from using state variables
erroneously, and taking the corresponding performance hit either
there, or by creating additional functions.

More information about the Python-ideas mailing list