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

Andrew Barnert abarnert at yahoo.com
Tue Mar 22 16:03:35 EDT 2016


On Mar 22, 2016, at 10:51, Michel Desmoulin <desmoulinmichel at gmail.com> wrote:
> 
> For every slice. We don't, because slicing is part of our standard data
> processing toolkit. And I think we can make it even better.
> 
> We already do in some places. E.G: range(10)[3:5] works while range()
> generate values on the fly. Why ? Because it's convenient, expressive
> and Pythonic.

Range is a sequence, just like list and tuple, not an iterator, like generator and list_iterator. So it supports slicing because sequences generally support slicing. Not because it's so convenient and expressive that it's worth making a special case, just because it follows the normal rules.

It's amazing to me that so many people still describe xrange/3.x range as an iterator or generator, and then draw further incorrect lessons from that. Yes, the docs were a bit confusing up to about 2.4, but how long ago is that now? Lazy sequences are still sequences. Strict non-sequences (like dict) are still not sequences. Lazy vs. strict has almost[1] nothing to do with sequence vs. iterator (or sequence vs. non-sequence collection, or collection vs. iterator, or anything else.)

---

[1]: Just "almost nothing" because you can't really write a strict generator--or, rather, you sort of can in a way, but it still has to look lazy to the user, so there's no point.



More information about the Python-ideas mailing list