[Python-ideas] Should range() == range(0)?

Georg Brandl g.brandl at gmx.net
Mon May 7 12:50:26 CEST 2012


On 05/07/2012 09:06 AM, Nick Coghlan wrote:
> On Mon, May 7, 2012 at 3:16 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> Also I can't remember seeing a plethora of use cases for
>> comparing range objects.
> 
> Most of the changes to range() in 3.3 are about making them live up to
> their claim to implement the Sequence ABC. The approach taken to
> achieve this is to follow the philosophy that a Python 3.3 range
> object should behave as much as possible like a memory efficient
> representation for a tuple of regularly spaced integers (but ignoring
> the concatenation and repetition operations that tuples support but
> aren't part of the Sequence ABC).
> 
> Having range() return an empty range in the same way that tuple()
> returns an empty tuple would be a natural extension of that
> philosophy.

For what gain?  At the moment, I cannot think of any arguments in favor
of the change, which is the point where arguments against it aren't
even needed to keep the status quo.

Ah yes: and I would rather have the bug

for i in range():   # <- "n" (or equivalent) missing

give me an explicit exception than silently "skipping" the loop.
After all, the primary use case for range() is loops, and we should not
make that use worse for the benefit of hypothetical other use cases.

Georg




More information about the Python-ideas mailing list