[Python-ideas] Generator syntax hooks?

Chris Angelico rosuav at gmail.com
Mon Aug 7 16:14:52 EDT 2017


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. I find this quite inefficient:
>
> {x for x in integers if 1000 <= x < 1000000} # never completes, because it's
> trying to iterate over all integers
>
> What if, somehow, object `integers` could hook the generator and produce the
> equivalent of {x for x in range(1000, 1000000)}, which does complete?
>
> What if, (x for x in integers if 1000 <= x < 1000000), was syntax sugar for
> (x for x in range(1000, 1000000))?
>
> (I like mathy syntax. Do you like mathy syntax?)

I don't. I prefer to stick with the syntax we already have. The
alternative is a more verbose way to identify a range, plus you need a
new global "integers" which implies that you could iterate over
"reals" the same way (after all, mathematics doesn't mind you working
with a subset of reals the same way you'd work with a subset of ints).
And good luck iterating over all the reals. :)

ChrisA


More information about the Python-ideas mailing list