[Python-ideas] for/else syntax

Yuvgoog Greenle ubershmekel at gmail.com
Fri Oct 2 17:42:48 CEST 2009

On Fri, Oct 2, 2009 at 5:35 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> I disagree that it makes sense. Why raise a SyntaxWarning for legal
> code? Shall we raise SyntaxWarning for the following as well?
> #1
> pass
> pass
> #2
> if False:
>    parrot()
> #3
> for x in []:
>    do_something()
> #4
> try:
>    something()
> except Exception:
>    raise

All of your examples do exactly what they appear to do. The for...else
construct that has no "break" appears to do something, yet does
nothing. I beg of you to try out "The Question Challenge" I posted
earlier. It's a win-win situation for you.

If you're still not convinced, check out this code that is legal, yet
appears to do something that it doesn't:

    z = math.sqrt(x / y)
except ZeroDivisionError, ValueError:
    print("Math error, try again")

That's a real life mistake many have seen occur. Python 3 fixed this
in a very elegant way. Now let's talk python 4.

More information about the Python-ideas mailing list