Because FLOSS is handy, isn’t it?

Labeled breaks in Python

Posted by isilanes on December 19, 2007

I am a recent fan of Python, a very neat scripting language.

One thing I miss from Perl is the availability of labeled breaks. What are those? Suppose you have two nested loops. When a condition is met in the inner loop, you want to exit both loops. With Python there is not straightforward way of doing it. Imagine we are reading an array of data, line by line and column by column, and we want to exit when meeting the first zero value. With Perl:

LINELOOP: foreach my $i (0..$lines)
  COLLOOP: foreach my $j (0..$columns)
     break LINELOOP unless $val[$i][$j];

A simple “break” will exit the innermost loop, but we can use a label to exit a specific loop. However, in Python there is no such a thing as a labeled loop, as explained in this PEP.

My rant is with the explanations given by van Rossum himself in Python mailing list to reject the change:

1. The complexity added to the language, permanently.
2. My expectation that the feature will be abused more than it will be used right.

Wow! Incredible reasons!

The first one is silly: other languages have it, and it has worked fine. Adding complexity to a tool for the sake of it is really stupid, I agree. But the fact is labeled breaks would be tremendously useful, so the increase in complexity would be justified. Surely a language that can only print “Hello world” would be less complex, yet of little use.

The second reason is absolutely over-the-shoulder-of-the-users. So now good old Guido must guide his sheep along the “correct” path, lest we get lost! He is punishing the good programmers by not giving them a useful tool, so that bad programmers are protected from their stupidity. It’s like not selling cars at all because some people drive while drunk.

Just my 2 cents…

3 Responses to “Labeled breaks in Python”

  1. Sharmila said

    “I’m rejecting it on the basis that code so complicated to
    require this feature is very rare. In most cases there are existing
    work-arounds that produce clean code, for example using ‘return’.
    While I’m sure there are some (rare) real cases where clarity of the
    code would suffer from a refactoring that makes it possible to use
    return, this is offset by two issues”

    this is the reason for Guido to reject the proposal. The need for such construct is very rare, that he opines it is not worth to add such a feature to the language and add complexity as a whole. In the two years I have been using python, I have come across such a need probably twice and using a flag sufficed fine🙂

  2. paddy3118 said

    Python has better exception handling than Perl and is arguably a better and more powerful way of handling such changes in control. Exceptions are not equivalent to multi-level breaks however so if you learnt how to use exceptions then you may structure your code in a different way than you would in Perl. Maybe the problem is in knowing both Perl and Python and mixing idioms?

    – Paddy.

  3. isilanes said

    Thanks Sharmila and Paddy3118. I actually love Python, and have few, if any, complains. Maybe in this case the problem is, as Paddy says, that one wants to use the ways of one language in the other :^)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: