[Python-ideas] Consider making enumerate a sequence if its argument is a sequence
Steven D'Aprano
steve at pearwood.info
Thu Oct 1 03:31:06 CEST 2015
On Wed, Sep 30, 2015 at 12:19:05PM -0700, Andrew Barnert via Python-ideas wrote:
[...]
> > FWIW: I don't think many people use the lazy sequence features
> > of range(), e.g. the slicing or index support. By far most
> > uses are in for-loops.
>
> I've used range as a sequence (or at least a reusable iterable, a
> sized object, and a container). I've answered questions from people on
> StackOverflow who are doing so, and seen the highest-rep Python
> answerer on SO suggest such uses to other people.
>
> I don't think I'd ever use the index method (although I did see one SO
> user who was doing so, to wrap up some arithmetic in a way that avoids
> a possibly off-by-one error, and wanted to know why it was so slow in
> 3.1 but worked fine in 3.2...), but there's no reason range should be
> a defective "not-quite-sequence" instead of a sequence. What would be
> the point of that?
There's also __contains__.
Personally, I don't like it, but using "n in range(a, b+1)" for testing
whether integer n falls within a particular range seems to be popular. I
don't know why they don't just write a <= n <= b, but it seems to be a
popular idiom for some weird reason.
--
Steve
More information about the Python-ideas
mailing list