Package: backports.range
Backports the Python 3.7rangeclass as a replacement for python 2.Xrangefunctions (and the Python pre-3.7rangeclass).
Therangeclass is similar toxrangein that its values are computed on demand -
however, therangeclass is also a lazy sequence:
it supports indexing, membership testing and other sequence features.Thus, it can be used to replace bothrangeandxrange.Table of ContentsFeaturesPerformanceCython OptimizationsCompatibilityStatus and ContributingNoticeFeaturesThis implementation provides all features introduced and documented in
python 3.6, and implements thecollections.abc.Sequenceinterface of Python 3.5[1]and later.
Therangeclass is available asbackports.range.range-
you can import it to a separate name or replace the builtinrangeand/orxrange.# import for explicit usage
from backports.range import range as range3
values = range3(499, 501)
# as a lazy, reusable iterable
print(', '.join(values), 'and', ', '.join(values))
# as a lazy sequence
print(500 in values, 501 in values)
print(values[2], len(values))All objects are available by default in a pure python implementation.
In addition, an optional, optimized implementation is available usingCython.PerformanceThebackports.rangeis adequate in performance for most applications.
It is comparable in iteration speed to builtins when usingPyPyorCython.
For small ranges of less than 1000 elements, there is some small overhead.
This should not be noticeable in all but the most high-performance applications.When using CPython (aka plainpython), pure python mode is considerably slower than the builtins.
Again, this should not matter for most applications, but the use ofCythonisstronglyadvised.Benchmark for[a for a inrange(...)]Interpretervs Builtin rangevs Builtin xrangePy250 - 10020 - 50Py325 - 30—Py2 + Cython3 - 61.0 - 3Py3 + Cython1.0 - 1.2—PyPy21.4 - 1.61.6 - 2.3PyPy31.0 - 1.1—Cython OptimizationsThe backport features aCythonimplementation.
It is transparently used when creating arangeclass.
It optimises operations which are purely in the Clong longrange.[2]AllCythonoptimizations are optional.
They are automatically made available ifCythonis installed.CompatibilityFeatures are tested against the Python 3.6 unittests forrange.The following python versions are tested explicitly:CPython (aka python):2.6,2.7,3.2,3.3,3.4,3.5,3.6,3.7-devCython: All versions supported by cpythonPyPy: pypy2, pypy3Some additional features are available for compatibility:Instances ofbackports.range.rangecompare equal to equivalentbuiltin.rangeinstances (new in Python 3.3)Theindexmethod is compliant with the Python 3.5+ specification ofcollections.abc.Sequence.[1]Some features depending on language features or other modules may not be
available or work differently:Comparingrangeagainst other types does not throwTypeErrorin python 2.X.Python implementations use custom optimisations at compile time when provably encountering abuiltinrange.
Thesecannotbe replicated by the backport.
Note that this is only noticeable in highly optimised code, which should be hand-optimised anyway.Status and ContributingFor all practical purposes, the backport is complete.
Therangeclass satisfies all unittest of Python 3.7, as well as several tests for compatibility.
It is a fully featured replacement suitable even for recent versions of Python.Performance should be more than adequate for any use case.
The backport is comparable or even equal in performance to builtins.If you encounter any problems, please head over to theissue tracker.
Feel free to submit a pull request if you think the codebase can be improved.NoticeThis packages includes parts of the python documentation (http://www.python.org)
Copyright (C) 2001-2016 Python Software Foundation.
Licensed under the Python Software Foundation License.[1](1,2)As of Python 3.6, the builtinrangeclass is not compliant
with the Python 3.5 specification ofcollections.abc.Sequence.
SeeIssue 28197[2]Thelong longtype should fill any current CPU architecture (read 64bit).
Since Python cannot parallelize instructions,
this should provide maximum range with optimal performance for small values.