[Python-ideas] Control Flow - Never Executed Loop Body

Sven R. Kunze srkunze at mail.de
Thu Mar 24 04:55:09 EDT 2016


On 24.03.2016 07:54, Sjoerd Job Postmus wrote:
> What worries me about this is that the next question is going to be: Why
> not have this on `with` suites, `if` suites, and so on. I've already
> seen remarks symmilar to that as replies.

My initial motivation is to support the "empty" use-case. I am not 
attached to any proposal as long as that is achieved. For one reason, I 
dislike to explain how to emulate the missing feature to others 
(especially when Django and jinja2 supports it) and for another I found 
usage for it for myself recently.

[offtopic]
I am curious now. What is the problem with providing exception handling 
to more than just try? Is it just because it complicates the language 
which you are used to? Or is there something really problematic I miss here?

> Especially because of your
> remark 'Which basically merges try and for', which adds to my worries.

This was to make people understand it more easily. :-)

> Consider the following:
>
>      for item in [0, 1, 2]:
>          raise StopIteration
>      except StopIteration:
>          # do after the loop
>          # Also after a `raise` inside the loop body?
>
>  From your statement 'merges try and for', it would seem as you now want
> the exception to be caught in this case as well.

That's interesting.  Yes, that could be confusing. But what can we do?

I cannot believe that we run out of ideas just for something that some 
simpler languages can handle easily. :-/

> Now, maybe you did not
> intend it as such, and really intended to only catch the exceptions
> raised by the iterable itself. But that was not quite clear to begin
> with. Because of this (possible) confusion, I'd really suggest slapping
> on another keyword to the `for` statement over allowing `except` clauses
> with limited use.
>
> Another reason is that
>
>      for item in [0, 1, 2]:
>          # do something
>      except StopIteration:
>          pass
>
> would do exactly the same as the `for` loop without the `except` clause,
> (due to the `pass`). Now this does not break the principle of 'one
> obvious way to do it', because for Pythonistas with experience, the
> `except` is superfluous. But would a novice know? (Assuming he started
> not by reading the Python docs, but by working on code written by
> another programmer)? Yes, RTFM, but more often you learn by reading
> other peoples code.

Noted but I wouldn't give too much weight to this issue. As others noted 
"else" is equally confusing.

Best,
Sven


More information about the Python-ideas mailing list