[Python-ideas] Generator syntax hooks?
Chris Barker
chris.barker at noaa.gov
Thu Aug 10 16:25:24 EDT 2017
On Thu, Aug 10, 2017 at 8:39 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> Also, there's a potential issue
> here - consider
>
> [expr for var in even_numbers() if is_odd(var) while var < 100]
>
> This is an infinite loop, even though it has a finite termination
> condition (var < 100), because we only test the termination condition
> if var is odd, which it never will be.
>
why is the termination only tested if teh if clause is True? Could then not
be processed in parallel? or the while first....
so maybe better to do:
[expr for var in even_numbers() while var < 100 if is_odd(var)]
Maybe it's just me, but I would certainly expect the while to have
precedence.
I guess I think of it like this:
"if" is providing a filtering mechanism
"while" is providing a termination mechanism
-- is there a use case anyone can think of when they would want the while
to be applied to the list AFTER filtering?
Obviously, this is a contrived example. And certainly "don't do that,
> then" is a valid response. But my instinct is that people are going to
> get this wrong - *especially* in a maintenance environment.
sure, but would there be an issue if teh while were given precedence?
Overall, I agree with Steven's point. It seems pretty obvious what the
> intention is, and while it's probably possible to construct examples
> that are somewhat unclear,
>
> 1. The mechanical rule gives an explicit meaning
> 2. People shouldn't be writing such complex comprehensions, so if the
> rule doesn't give what they expect, they can always rewrite the code
> with an explicit (and clearer) loop.
>
me too -- a direct translation to a for loop isn't necessary to understand
how it works.
-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/20170810/671d5703/attachment-0001.html>
More information about the Python-ideas
mailing list