[Python-ideas] Control Flow - Never Executed Loop Body
Sven R. Kunze
srkunze at mail.de
Sun Mar 20 14:12:58 EDT 2016
Hi Python-ideas,
this is a follow up from an thread of Python-List:
https://mail.python.org/pipermail/python-list/2016-March/705205.html
What do you think about the following proposal?
*Abstract*
Control structures such as "for", "while" and "try" feature some various
additional clauses being executed under certain conditions. This
proposal adds another extra clause to "for" and "while" in order to
provide an easy way to discover whether the body of a loop as never been
executed at all.
*Motivation**
*Coming from the Web application development, we periodically can see a
benefit of a hypothetical "empty" clause of Python's for loop like this:
for item in my_iterator:
# do per item
empty:
# do something else
The same goes for "while" although there the choice of the name "empty"
might be questionable. "empty" refers to the involved iterable which is
not really existing in case of "while". However, there is not need of
introducing inconsistencies among the loop constructs as both feature
the "else" clause as well. Better suggestions are welcome; also see the
keyword section.
*Keyword*
Keywords under consideration have been in (an attempt of dispassionate)
order of preference:
1) empty -> most obvious, most people responded with a solution to solve
the intended problem
2) else -> already taken but to some would be the preferred one
3) or -> as alternative if a new keyword is too much
4) except -> as alternative if a new keyword is too much
5) instead -> less obvious and new keyword
*Use-Cases*
1) As humans mostly have a hard time to discover the difference between
*nothing**as error* and *nothing**as really empty*, GUI/Web interfaces
display an explicit notice in case of empty lists to signify the correct
execution of a process/action/search or the other case.
2) Single way and no boilerplate of handling empty loops. As the
discussion on Python-List showed are there quite some number of
different ways of achieving this kind of functionality.
3) Less errors in development and maintenance. Some of the proposed
solutions would not work in some circumstances (either during
development or depending on the type of the items) which can hide errors.
*Implementation*
To be done.
*Maintainer*
Me.
*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.
*Related Work**
*This proposal is partly inspired by template engines like Django or
jinja2 which both already provide such facility.
Django:
{% for item in my_iterator %}
...
{% empty %}
...
{% endfor %}
jinja2:
{% for item in my_iterator %}
...
{% else %}
...
{% endfor %}
Best,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160320/5c348f88/attachment-0001.html>
More information about the Python-ideas
mailing list