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

Terry Reedy tjreedy at udel.edu
Mon May 7 05:22:18 CEST 2012

On 5/6/2012 7:52 PM, Devin Jeanpierre wrote:
> On Sun, May 6, 2012 at 5:24 PM, Terry Reedy<tjreedy at udel.edu>  wrote:
>> Another advantage of doing this, beside consistency, is that it would
>> emphasize that range() produces a re-iterable sequence, not just an
>> iterator.

My apology for mis-writing that. A range is a non-iterator, re-iterable 
sequence rather than an iterator.

> How? The empty sequence is the exact case where reiterable objects and
> iterators have identical iteration behavior. (Both immediately stop
> every time you try.)

That is also true of empty tuples, lists, sets, and dicts. An iterator 
can only be used to iterate - once. Non-iterator iterables (usually) 
have other behaviors.

>> Possible objections and responses:
>> 1. This would slightly complicate the already messy code and doc for
>> range().
>> Pass, for now
> By this, do you mean don't write new documentation?

No, it means I was defering discussing this possible objection unless 
someone raises it as a show-stopper, or it becomes the last issue. The 
current messiness is that the signature in the doc "range([start], 
stop[, step])" is non-standard in that it does not follow the rule that 
optional parameters and arguements follow required ones. It would 
perhaps be more accurate, but also possibly more confusing, to give it 
as "range(start_stop, [[stop], [step])", where start_stop is interpreted 
as start if stop is given and stop if stop is not (otherwise) given. 
Either version would just need an outer '[]' added: "range([[start], 
stop, [step]])" and a note "If no arguments are given, return range(0)."

For a Python version, adding "= 0" to start_stop in the header should be 
sufficient. But I do not know how the C version works.

> Most of the other types are useful as parameters to something such as
> collections.defaultdict.

I admit range() would be seemingly useless there.

> Although, I actually like this idea, because it feels more consistent.
> I imagine that isn't a good reason to like things though.

I believe, though, it was a reason for the consistency of everything 
other than range.

Terry Jan Reedy

More information about the Python-ideas mailing list