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

Georg Brandl g.brandl at gmx.net
Mon May 7 00:24:21 CEST 2012


On 05/06/2012 11:24 PM, Terry Reedy wrote:
> It is a general principle that if a built-in class C has a unique (up to 
> equality) null object, then C() returns that null object.
> 
>  >>> for f in (bool, int, float, complex, tuple, list, dict, set, 
> frozenset, str, bytes, bytearray):
> 	print(bool(f()))
> 
> # 12 lines of False
> 
> Some imported classes such as fractions.Fraction and collections.deque 
> can be added to the list.
> 
> I add 'up to equality' because in the case of floats, 0.0 and -0.0 are 
> distinct but equal, and float() returns the obvious 0.0.
>  >>> 0.0 == -0.0
> True
>  >>> m.copysign(1, 0.0)
> 1.0
>  >>> m.copysign(1, -0.0)
> -1.0
>  >>> m.copysign(1, float())
> 1.0
> 
> The notable exception to the rule is
>  >>> range()
> Traceback (most recent call last):
>    File "<pyshell#0>", line 1, in <module>
>      range()
> TypeError: range expected 1 arguments, got 0
>  >>> bool(range(0))
> False
> 
> It is true that there are multiple distinct null range objects (because 
> the defining start,stop,step args are kept as attributes) but they are 
> all equal.
>  >>> range(1,1) == range(0)
> True
> 
> range(0) == range(0, 0, 1) would be the obvious choice for range().
> 
> Another advantage of doing this, beside consistency, is that it would 
> emphasize that range() produces a re-iterable sequence, not just an 
> iterator.
> 
> Possible objections and responses:

[1. - 6.]

7. The "default value" is only really useful for types that are best
described as "data-like".  range is not a data-like type, it's a helper
for iteration, just as filter or dictviews aren't data-like.

Georg





More information about the Python-ideas mailing list