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

Chris Barker - NOAA Federal chris.barker at noaa.gov
Sun Mar 27 23:30:47 EDT 2016


Oops, hit send by accident.

>
>>> for line in the_file[:10]:
>>>  ...
>>>
>>> arrgg! files are not indexable!.
>>
>> No, they are *enumerable*:
>>
>>   for i, line in enumerate(the_file):
>>       if i >= 10: break
>>       ...
>>
>> I guess your request to make iterators more friendly is a good part of
>> why enumerate() got promoted to builtin.
>
> Sure, but you have to admit that the slicing notation is a lot
> cleaner. And I don't WANT to enumerate -/ I want
I want to iterate only the first n items -- that may be a common
enough use case for a concise notation.

>> takewhile or dropwhile from itertools is your friend.  (I have no
>> opinion -- not even -0 -- on whether promoting those functions to
>> builtin is a good idea.)

Well this thread started with those ideas ....

My point is still : making working with iteratables as easy and
natural as sequences is a good direction to go.

-CHB



>>
>>> But aside from that -- just the idea that looking at how to make
>>> iterable a more "natural" part of the language is a good thing.
>>
>> I think it's from Zen and the Art of Motorcycle Maintenance (though
>> Pirsig may have been quoting), but I once read the advice: "If you
>> want to paint a perfect painting, make yourself perfect and then paint
>> naturally."  I think iterators are just "unnatural" to a lot of
>> people, and will remain unnatural until people evolve.  Which they may
>> not!
>>
>> Real life "do until done" tasks are careers ("do ... until dead") or
>> have timeouts ("do n times: break if done else ...").  In computation
>> that would be analogous to scrolling a Twitter feed vs. grabbing a
>> pageful.  In the context of this discussion, a feed is something you
>> wait for (and maybe timeout and complain to the operator if it blocks
>> too long), while you can apply len() to pages.  And you know which is
>> which in all applications I've dealt with -- except for design of
>> abstract programming language facilities like "for ... in".  The point
>> being that "real life" examples don't seem to help people's intuition
>> on the Python versions.
>>
>>
>>


More information about the Python-ideas mailing list