[Python-3000] range() issues

Facundo Batista facundobatista at gmail.com
Sat Apr 26 20:49:19 CEST 2008


2008/4/26 Alexander Belopolsky <alexander.belopolsky at gmail.com>:

> > What is range()?
>  >
>  > help(range) shows me that range "Returns an iterator that generates
>  > the numbers in the range on demand."
>
>  This is not correct in 3.x: range does not return an iterator.  There is an
>  iterator similar to range in itertools: count.   I would not mind adding
>  optional step and stop arguments to it.

I took that string doing help(range) in the py3k branch, r62509, is it a bug?

Which should the range() definition be, in your words?


>  > Ahá! So, as ints are unbound in Python, I could easily do:
>  >
>  > >>> r = range(1,1000000000000000000000)
>
>  The problem with supporting this is that len(r) will raise overflow error.
>  It would be nice to get rid of the limitation on len(), but it will be hard
>  and may not be possible to do efficiently.

Maybe len() should be removed? Maybe indexing? I don't know: I don't
know what range() is. I mean, I took the previous definition from the
actualy Py3k, but you say it's wrong.

I think that we should first define the range() semantic, what is core
to it and what would be a nice thing to have but is not mandatory, and
then try to comply.

At this moment I stopped writing this mail, and I went to code a
Range() class to have the semantics that we're seeking here (it's
attached), and I couldn't finish it 100% because of a len() behaviour
that I'm including here, because it's related to what we're discussing
here:

>>> class C:
...     def __len__(self):
...             return 100000000000000000000000000000
...
>>> c = C()
>>> len(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    OverflowError: Python int too large to convert to C ssize_t

>From an external point of view, and knowing that ints are unbound, why
should I have an error here?

Thanks!

-- 
. Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: myrange.py
Type: text/x-python
Size: 1482 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-3000/attachments/20080426/86e1b395/attachment.py>


More information about the Python-3000 mailing list