[Python-ideas] Integrate some itertools into the Python syntax

Chris Angelico rosuav at gmail.com
Fri Mar 25 18:19:15 EDT 2016


On Sat, Mar 26, 2016 at 9:16 AM, Michel Desmoulin
<desmoulinmichel at gmail.com> wrote:
> Le 25/03/2016 17:53, Nikolaus Rath a écrit :
>> On Mar 25 2016, Michel Desmoulin <desmoulinmichel-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org> wrote:
>>> It's also very convenient for generator expressions:
>>>
>>> # get random numbers between 0 and 100000, and square them
>>> # remove all numbers you can't devide by 3
>>> # then sample 100 of them
>>>
>>> numbers = (x * x for x in random.randint(100000) if x % 3 == 0)
>>> for x in numbers[:100]:
>>>     print(x)
>>
>> If there has ever been an example that I hope to never, ever see in
>> actual code than it's this one. If you have to make up stuff like that
>> to justify a proposed feature, then that does not bode well.
>
> The (x * x for x in random.randint(100000) if x % 3 == 0) is just here
> to stand for "rich generators". The example is artificial, but every
> advanced python code out there use generator.
>
> The feature is on the for loop, which is simple and elegant.
>
> What you're doing here is trolling, really.

No, it's not trolling. But there's a difficult balance to strike when
giving examples of a new feature; too simple and there's no
justification for the feature, too complex and it's a poor
justification ("if you have to make up stuff like that"). Trying to
see past the example, generalizing to code that you might yourself
write, isn't always easy.

ChrisA


More information about the Python-ideas mailing list