Skip to main content

Backport of the python 3.X `range` class

Project description

Unit Tests Code Coverage

PyPI Package PyPI Status PyPI Versions PyPI Implementations

Backports the Python 3.7 range class as a replacement for python 2.X range functions (and the Python pre-3.7 range builtin). The range class is similar to xrange in that its values are computed on demand - however, the range class is also a lazy sequence: it supports indexing, membership testing and other sequence features.

Thus, it can be used to replace both range and xrange.

Features

This implementation provides all features introduced and documented in python 3.6, and implements the collections.abc.Sequence interface of Python 3.5 [1]. The range class is available as backports.range.range - you can import it to a separate name or replace the builtin range and/or xrange.

# 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, there are optional, optimized implementations using Cython.

Performance

The backports.range is adequate in performance for most applications. It is comparable in iteration speed to builtins when using PyPy or Cython. 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 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

Cython Optimizations

All Cython optimizations are optional. They are automatically made available if Cython is installed.

  • Iteration in the C long long range [-9223372036854775807, 9223372036854775807]

Compatibility

  • Features are tested against the Python 3.6 unittests for range.

  • 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-dev

    • Cython: All versions supported by cpython

    • PyPy: pypy2, pypy3

  • Some additional features are available for compatibility:

    • Instances of backports.range.range compare equal to equivalent builtin.range instances (new in Python 3.3)

    • The index method is compliant with the Python 3.5+ specification of collections.abc.Sequence. [1]

  • 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.

Notice

This 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backports.range-3.7.0.tar.gz (10.9 kB view details)

Uploaded Source

File details

Details for the file backports.range-3.7.0.tar.gz.

File metadata

File hashes

Hashes for backports.range-3.7.0.tar.gz
Algorithm Hash digest
SHA256 f46f447eddc05c61302e718ed36c358d629f0fb211c79f978c39ea324d1b7928
MD5 1b3cc0b1e2603e8a115a14a474fd0285
BLAKE2b-256 d7a5bc50ba8dfc3dd97f6470745131076172d8b6734fcf7472bb17cd40b3a846

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page