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

Chris Barker chris.barker at noaa.gov
Mon Mar 21 12:31:23 EDT 2016


On Mon, Mar 21, 2016 at 1:01 AM, Stephen J. Turnbull <stephen at xemacs.org>
wrote:

> AFAICS the OP's idea has a trivial and efficient expansion:
>

I'll take your word for it that this is efficient, and it is trivial in
amount of code to write, but it is not the least bit trivial to read or
write. It took me a good while to understand what it did, and how it works,
and I never would have thought of it myself.


>     item = _sentinel = object()
>     for item in iterable:
>         # do for each item
>     if item is _sentinel:
>         # do exactly when iterable raises StopIteration on the first pass
>

I would have done the klunky thing:

_sentinal = False
for item in iterable:
    _sentinel = True
   # do something
if not _sentinel
    # do something else if the loop never ran

almost as compact, less efficient and enough to make me see why the OP
want's something new.

Howver, the motivating example was "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."

Sure -- but this seem to be mixing validation with processing code -- you
really want to validate your inputs first.

AND: this would mostly be containers, rather than iterables, so the idiom
of:

if input:
    for item in input:
        # do something
else:
    raise EmptyInputError

works fine.


All that being said:

how about  "elempty"?, to go with elif?

-CHB


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160321/d32b28bd/attachment.html>


More information about the Python-ideas mailing list