[Python-3000] Using range()
Mark Dickinson
dickinsm at gmail.com
Thu Apr 24 22:01:13 CEST 2008
On Thu, Apr 24, 2008 at 11:50 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> There's definitely some bugs in this area of the range object code though:
>
> >>> x = range(2**33, 2)
> >>> len(x)
> 0
> >>> x[0]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> IndexError: range object index out of range
>
Hmm. I'm not seeing the bug here. What am I missing?
It seems to me that there are two reasonable behaviours
for range(a, b) when b is less than a: return an 'empty' range,
as in the example above, or raise a ValueError;
I can see arguments for both behaviours. But one good
argument in favour of the current behaviour is that
xrange(a, b) in Python 2.x currently returns an empty range
when b < a:
>>> xrange(3, -2)
xrange(3, 3)
> I also believe that the OverflowError from doing len(self) while attempting
> to index into the range should be intercepted and converted to something
> more meaningful for the actual operation requested by the programmer (e.g.
> "ValueError: Cannot index range objects with sys.maxsize or more elements")
>
Agreed. Though if it's easy to fix things so that range(a, b)[n]
always 'just works' for any integer a <= n < b, and if the fix
doesn't have any significant performance impact, wouldn't that
be even better?
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-3000/attachments/20080424/6975aa54/attachment.htm>
More information about the Python-3000
mailing list