[Python-ideas] Generator syntax hooks?
Chris Barker
chris.barker at noaa.gov
Fri Aug 11 15:55:44 EDT 2017
[...]
> result = []
> for x in sequence:
> if condition:
> result.append(expression)
> else:
> break
>
> which could be written
>
> [expression for x in sequence if condition break]
>
> It's what I thought too. Adding a `while` clause here just overly
> complicates the understanding of the comprehension. The `break` keyword is
> already easily understandable and helps to map the comprehension with the
> plain for-loop (I like this mapping for its reverse counterpart, as I often
> start with plain for-loops to rewrite them later to comprehensions when it
> makes sense).
>
having the "if condition" there seems confusing to me, particularly if you
want an if condition as a filter as well:
[expression for x in sequence if condition1 if condition2 break]
which makes me want:
[expression for x in sequence if condition1 breakif condition2]
adding another keyword is a pretty big deal though!
would it be possible to add a keyword ("breakif" in this case) that was
ONLY legal in comprehensions?
Though I still dop'nt think using "while" would really introduce that much
confusion -- sure, it doesn't introduce a new loop, but, as someone pointed
out earlier in this thread it really is only changing from a:
"while do"
to a
"do while"
construct -- so means pretty much the same thing.
I agree that scanning a code base to see if there really are many loops in
practice that could use this construct would be a good way to see if there
is any point.
And it would also be interesting to do a survey of "random" folks as to how
they would interpret such a construct -- it's pretty hard for a small group
to know what is and isn't "confusing"
-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/20170811/dcfd92a7/attachment-0001.html>
More information about the Python-ideas
mailing list