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

Koos Zevenhoven k7hoven at gmail.com
Tue Mar 22 16:13:03 EDT 2016


On Tue, Mar 22, 2016 at 9:45 PM, Michel Desmoulin
<desmoulinmichel at gmail.com> wrote:
>
>
> Le 22/03/2016 20:26, Koos Zevenhoven a écrit :
>> On Tue, Mar 22, 2016 at 9:10 PM, Koos Zevenhoven <k7hoven at gmail.com> wrote:
>>> 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.
>>>
>>
>> I should add, though, that the the function should probably return
>> list(f[begin, end][:10000]) or something, before the file f is closed.
>>
>>  - Koos
>
> Yeah the semantic of my function is wrong. It should rather expect a
> file like object as a parameter to avoid this bug.

Yeah, and perhaps be a generator function and yield from
f[begin:end][:10000] instead of returning it.

But beginners may end up trying this. It may be better to have them as
functions (either in itertools or perhaps as attributes of iter),
which may help them see what's actually going on. Or help them see
that they actually don't know how it works and should just make a for
loop for this to be more clear to them.

 - Koos


More information about the Python-ideas mailing list