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

Koos Zevenhoven k7hoven at gmail.com
Tue Mar 22 15:10:06 EDT 2016


On Tue, Mar 22, 2016 at 8:41 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 03/22/2016 10:51 AM, Michel Desmoulin wrote:
>
>> def foo(p):
>>      with open(p) as f:
>>          def begin:
>>              return x == "BEGIN SECTION"
>>           def end:
>>              return x == "STOP"
>>          return f[begin, end][:10000]
>>
>> It's very clean, very convenient, very natural, and memory efficient.
>
>
> Except the 10,000 limit doesn't happen until /after/ the end block is
> reached -- which could be a million lines later.


if f[begin, end] is a generator, the 10000 limit may happen before the
end block is reached, which I think was the point.

Python 3 does give generators and iterators a more central role than
Python 2. One thought that comes to mind is to add some stuff from
itertools as attributes of iter. For example: iter.slice(...). Kind of
like a factory method, although iter is not really a type.

 - Koos


More information about the Python-ideas mailing list