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

Andrew Barnert abarnert at yahoo.com
Sun Mar 20 16:16:50 EDT 2016


On Mar 20, 2016, at 11:12, Sven R. Kunze <srkunze at mail.de> wrote:
> 
> Issues
> People I talked to suggested "else" as an alternative to "empty" because "empty is not quite clear". Unfortunately, "else" is already taken and is itself not quite clear. "then" has been proposed as an alternative for "else" in an attempt for proper understanding of "else". If that would be an accepted proposal, it would make room for "else" to be used for the usage of the "empty keyword proposed here.

Besides the backward compatibility issue, changing "else" to "then" would be horribly confusing. I suspect anyone who thinks that would be an improvement doesn't actually understand or like for...else, and they'd be happier just eliminating it, not renaming it.

An else clause is testing that no break was hit inside the loop. Look at a typical example:

    for elem in seq:
        if good(elem): break
    else:
        raise ValueError

The word "then" would make no sense there. "Find the first good element, else raise an exception" makes sense; "find the first good element, then raise an exception" means the opposite of what we want.

Anyway, none of this speaks for or against your main proposal, it just means (at least in my opinion) that this alternative option is off the table.

More generally, I think if this feature were to be added, "empty" is a reasonable name. The idiomatic way to write it today is something like:

    elem = empty = object()
    for elem in seq:
        do_stuff(elem)
    if elem is empty:
        do_empty_seq_stuff()

So you're basically looking for syntactic sugar that abbreviated "if ... is empty:", right?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160320/05a4ce32/attachment.html>


More information about the Python-ideas mailing list