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

Ethan Furman ethan at stoneleaf.us
Mon Mar 28 12:45:23 EDT 2016


On 03/28/2016 09:34 AM, Chris Angelico wrote:
> On Tue, Mar 29, 2016 at 3:29 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> Iter*ables* are easy to slice (think list, tuple, range, etc.).
>>
>> Iter*ators* are the tricky ones.  And yes, there is no way to slice an
>> iterator without changing its state.
>
> *Some* iterables are easy to slice. Some are not. A dictionary is
> perfectly iterable, but it doesn't make a lot of sense to slice it.
> Are you slicing based on keys, or sequence (like paginating the dict),
> or something else? That's why iterable slicing needs to be handled by
> the type itself - a range knows how to produce a subrange, a list
> knows how to produce a sublist.

 From the glossary  [https://docs.python.org/3/glossary.html]

   iterable
      An object capable of returning its members one at a time.

Very good point, Chris, thanks for the correction.


So the key point then is that objects with a `__getitem__` that is based 
on offsets `0` to `len(obj)-1` are easily sequenced, all others may as 
well be wrapped in `iter()` calls.

--
~Ethan~


More information about the Python-ideas mailing list