for / while else doesn't make sense

Michael Selik michael.selik at gmail.com
Fri May 20 11:26:59 EDT 2016


On Thu, May 19, 2016 at 1:04 PM Ian Kelly <ian.g.kelly at gmail.com> wrote:

> On Thu, May 19, 2016 at 10:31 AM, Herkermer Sherwood <theherk at gmail.com>
> wrote:
> > Most keywords in Python make linguistic sense, but using "else" in for
> and
> > while structures is kludgy and misleading. I am under the assumption that
> > this was just utilizing an already existing keyword. Adding another like
> > "andthen" would not be good.
>
> "else" makes sense from a certain point of view, but I think that
> logic may not be communicated well. At the start of each loop
> iteration, the loop construct makes a test for whether the loop should
> continue or not. If that test ever fails (i.e. if the condition of the
> while loop is false), the else block is executed instead. So you can
> think of it as a repeated if-else where the else block has the
> additional effect of exiting the loop.
>
> > But there is already a reserved keyword that would work great here.
> > "finally". It is already a known keyword used in try blocks, but would
> work
> > perfectly here. Best of all, it would actually make sense.
> >
> > Unfortunately, it wouldn't follow the semantics of
> try/except/else/finally.
>
> "finally" in exception handling denotes a block that is *always*
> executed. Using it for a block that is only sometimes executed would
> dilute that meaning.
>

It's unfortunate that so many people responded so quickly, since Ian's
explanation was so clear (I thought). For further clarity, I'll write out
the implicit if-statement that Ian described, though in my case it'd be at
the end of the block, somewhat like a do-while:

    IF keep_looping()
    THEN GOTO LOOP_START
    ELSE GOTO LOOP_COMPLETED

Also, Nick Coghlan has a good post about this (
http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html
)



More information about the Python-list mailing list