[Python-ideas] for/else syntax

Yuvgoog Greenle ubershmekel at gmail.com
Sat Oct 3 05:43:02 CEST 2009


*snip* *snip*
On Fri, Oct 2, 2009 at 6:23 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, 3 Oct 2009 01:42:48 am Yuvgoog Greenle wrote:
>> The for...else construct that has no "break" appears to do something,
>> yet does nothing.
>
> Nonsense.
>

*snip* *snip*

On Fri, Oct 2, 2009 at 6:23 PM, Steven D'Aprano <steve at pearwood.info> wrote:
>> try:
>>     z = math.sqrt(x / y)
>> except ZeroDivisionError, ValueError:
>>     print("Math error, try again")
>
> Irrelevant. We're not talking about the syntax of the except statement.
>
> Why would confusing syntax there convince me that a *completely
> different* statement was also confusing? That's like saying, "If you
> don't agree that chocolate tastes terrible, have a taste of this
> cow-vomit. Terrible, right? So now we agree, chocolate tastes
> terrible."
>


You present 2 claims so I present 2 answers, please don't mix the two up:
1. Does "for...else" do what it appears to be doing?
2. Does python allow syntax that's likely to cause confusion or mistakes?

A summary of the long answers that follow:
1. In certain cases, no it doesn't. In a few cases showcased here -
comments are the only way to make the code readable.
2. Python doesn't impose silly syntax limitations, but it can and has
deprecated a "legal" syntax if a more readable, not misleading,
alternative can be found/created/used.

----------------
1. Does "for...else" do what it appears to be doing?

This is subjective matter so of course we can agree to disagree. Let
me entertain you Steven.

First of all I suggest reading through this thread and looking for
code examples with for..else that were refuted, retracted,
misunderstood or wrong. I can count at least 3-4 instances from here
(Carl noted Gerald's but there were others as well). This isn't
python-noobs either, it's python-ideas. These are good pythonistas
that were mislead by the for..else syntax.

Secondly, I recommend anybody interested to go ahead and try "The
Question Challenge". I'll rephrase it a bit to accommodate Steven's
claims.

Ask a person who knows python the following questions:
Question #1: Do you know what happens when an "else" appears after a "for" loop?
    if they answer yes then say never mind.
    otherwise: continue to question #2.
Question #2: When does the code in the "else" block execute? Of course
you don't know so it's ok to guess.
    If they answer "when a break didn't occur", I'm willing to give
you, the asker, up to 100 lines of patch code in whatever python
project you want.
    If they answer "only when the for loop didn't run or was an empty
list" then you don't owe me anything.

To remove any doubt you can ask the follow up question "So either the
for loop block runs or the else block runs, but in no way both?"

If you want to have some good conversation, tell them what python
really does with that "else".

Please don't misinterpret this Question Challenge as an attack on
Python. I love Python and I have no other language. At least no other
language I enjoy using :)

----------------
2. Does python allow syntax that's likely to cause confusion or mistakes?

Short answer - preferably no. It's an issue of practicality vs purity
etc. Also, python tries not to have silly limitations.

> Steven D'Aprano 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

It would be a silly limitation to not allow:

pass
pass

It would be a silly limitation because there's nothing wrong with
"pass-pass". Concerning our specific discussion - "pass-pass" isn't
likely to cause any confusion or mistakes.

So what's a "syntax that's likely to cause confusion or mistakes"?

For example, the old python 2 exception instance catcher:
try:
    # code
except Exception, e:
    # code

Sounds ok, but now check out:

try:
    # code
except ExceptionClassA, ExceptionClassB:
    # handle errors of both exception types

That usage of the syntax is wrong because ExceptionClassB wouldn't be
caught and the exception instance would be named ExceptionClassB. This
appears to do one thing (catch A and catch B), yet does something
completely different (catch only A as B). Python 3 FTFY with "as". So
the same code in the new syntax would be:

try:
    # code
except ExceptionClassA as ExceptionClassB:
    # handle errors of both exception types.

The fact that ExceptionClassB is an instance is much more obvious and
making mistakes is harder. So yeah, python tries to avoid syntax that
may cause mistakes, in other words, the python syntax strives to be as
expressive as reasonably possible.

Note a few things please:
1. The python 2 syntax was legal
2. For python 3 it was declared an illegal syntax.
3. It was very easy to learn the correct python 2 syntax and not make
mistakes but still it was fixed for py3k.
4. Doing crazy things that made no sense became a lot harder. I know
I'm happy for that.

I hope it's clearer how this example is relevant to "for..else" now.
The point is if the "except" syntax can change from 2to3 to make it
more readable and exact, so can the for..else syntax change from 3to4.
Changing for..else would be done for very similar reasons.

And don't get me wrong, I like nipples, chocolate and cow vomit as
much as the next guy. It's just I would really love to discuss if
and/or how this problem can be resolved.



More information about the Python-ideas mailing list