[Python-ideas] for/except/else syntax

geremy condra debatem1 at gmail.com
Thu Oct 8 02:57:49 CEST 2009


On Wed, Oct 7, 2009 at 5:58 PM, Steven D'Aprano <steve at pearwood.info> wrote:

> On Thu, 8 Oct 2009 04:27:22 am MRAB wrote:
>
> > Perhaps:
> >
> > for i in SEQ:
> >      A
> > elif not break:
> >      C # suite_if_loop_is_not_broken
> > elif pass:
> >      B  # suite_if_loop_body_is_not_executed
>
> Here's my executive summary, for those in a hurry:
>
> -1 on any variation like "else not break" or "elif not break".
>
> +1 on aliasing "for...else" to "for...then", with identical semantics,
> with eventual deprecation and removal of for...else some time in the
> indefinite future. (Likewise for while...else.)
>
> -0 on introducing a clause that executes if the loop is never executed
> at all.
>
>
> And here are the details:
>
> Any variation like "else not break" or "elif not break" makes it look
> that you are testing a flag. If you think for...else is confusing
> now, I guarantee that this will be nothing compared to the confusion
> people will feel when they discover this (proposed) thing that looks
> like testing a flag but isn't. People will wonder where this break
> flag is set, and why they can't set it themselves, or even access it
> like any other object.
>
> (Before anyone suggests that perhaps we should introduce a global
> variable called "break", and allow the caller to read/write to it
> outside of the for loop, remember that break is a statement. It would
> be too abominable for words to have break a global variable and a
> statement at the same time.)
>
> Assuming there is consensus that for...else is confusing, my
> suggestion is to add an alias "then":
>
> for x in seq:
>    ...
> then:
>    suite
>
> The behaviour of "then" is identical to the current behaviour
> of "else", it's just another way of spelling it. Assuming consensus,
> eventually else would be deprecated and then removed.
>
> This has the advantage of a more natural interpretation. It seems more
> natural to say:
>
>    run the loop, then execute suite (unless there was a break)
>
> instead of
>
>    run the loop, optionally break, else execute suite.
>
>
>
> If you need to distinguish between "loop ran" and "loop didn't run at
> all", you don't need syntax for that, you can use the sentinel trick
> Ilya suggested. (You don't even need a special sentinel, just use
> None, provided you know None is not in the sequence you're iterating
> over.) But if this was so common a use-case that we needed syntax for
> it (and I don't believe it is), then I'd suggest:
>
> for x in seq:
>     suite A
> then:
>     suite B
> otherwise:
>     suite C
>
> I'm not really enamoured of "otherwise". I'd prefer to use "else" for
> the case of "the loop was never entered at all", but of course that
> is impossible given that it currently has another meaning.
>
>
>
> --
> Steven D'Aprano
>

Sounds good to me.

Geremy Condra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20091007/eaf8c400/attachment.html>


More information about the Python-ideas mailing list