Backport of the python 3.X `range` class
Backports the python 3.X range class as a replacement for python 2.X range functions. The range class is similar to xrange in that its values are computed on demand - however, the range class is actually a lazy sequence: it supports indexing, membership testing and other sequence features.
Thus, it can be used to replace both range and xrange.
This implementation provides all features introduced and documented in python 3.3.
All objects are available by default in a pure python implementation. In addition, there are optional, optimized implementations using Cython.
The backports.range is adequate in performance for most applications. It is comparable in speed to builtins when using PyPy or Cython, but somewhat slower for small ranges of less than 1000 elements. This should not be noticeable in all but the most high-performance applications.
When using CPython (aka plain python), pure python mode is considerably slower than the builtins. Again, this should not matter for most applications, but the use of Cython is strongly advised.
Benchmark for [a for a in range(...)]
|Interpreter||vs Builtin range||vs Builtin xrange|
|Py2||50 - 100||20 - 50|
|Py3||25 - 30||—|
|Py2 + Cython||3 - 6||1.0 - 3|
|Py3 + Cython||1.1 - 3||—|
|PyPy2||1.6 - 2.5||1.6 - 2.3|
|PyPy3||0.9 - 1.2||—|
- Features are tested against the Python 3.6 unittests for range.
- The following python versions are tested explicitly:
- Some features depending on language features or other modules may not be
available or work differently:
- Comparing range against other types does not throw TypeError in python 2.X.