[Python-ideas] Generator syntax hooks?

Chris Barker chris.barker at noaa.gov
Mon Aug 7 19:06:32 EDT 2017


On Mon, Aug 7, 2017 at 4:14 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Tue, Aug 8, 2017 at 5:30 AM, Soni L. <fakedme+py at gmail.com> wrote:
> > The generator syntax, (x for x in i if c), currently always creates a new
> > generator.


that's what it's for -- I'm confused as to what the problem is.


> > {x for x in integers if 1000 <= x < 1000000} # never completes, because
> it's
> > trying to iterate over all integers
>

this is a set comprehension -- but what is "integers"? is it a generator?

in which case,  it should take an argument so it knows when to end. Or if
it's really that symple, that's what range() is for.


However, similarly, I find that sometimes I want to iterate over a slice of
a sequence, but do'nt want to actually make the slice first.

So there is itertools.islice()

If "integers" is a sequence:

{x for x in integers[1000:10000]}

makes an unneeded copy of that slice.

{x for x in itertools.islice(integers, 1000, 10000)}

will iterate on the fly, and not make any extra copies.

It would be nice to have an easier access to an "slice iterator" though --
one of these days I may write up a proposal for that.

-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/20170807/b2fc4a8f/attachment.html>


More information about the Python-ideas mailing list