[Python-3000] Should len() clip to sys.maxsize or raiseOverflowError?

Nick Coghlan ncoghlan at gmail.com
Tue Sep 2 23:57:59 CEST 2008


Guido van Rossum wrote:
> The only time when __len__ can be larger than sys.maxsize is when the
> class implements some kind of virtual space where the values are
> computed on the fly. In such cases trying to walk over all values is
> bound to take forever, and the length is likely not of all that much
> interest to the caller -- but sometimes we may need to pass such an
> object to some library code we didn't write that is making some
> trivial use of len(), like the examples I gave before.
> 
> That said, I would actually be okay with the status quo (which does
> raise an OverflowError) as long as we commit to fixing this properly
> in 2.7 / 3.1, by removing the range restriction (like we've done for
> other int operations a long time ago).

For those that haven't been following issue 2690, the latter paragraph
will make it much easier to turn range() into a proper representative of
collections.Sequence.

I don't actually see any huge technical problems with implementing this
- we're just going to have to add a second C level method slot that uses
the unaryfunc signature (returning PyObject *) for a "virtual length"
method in addition to the existing mp_length and sq_length (which return
PySsize_t).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
            http://www.boredomandlaziness.org


More information about the Python-3000 mailing list