A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python.github.io/peps/pep-0204/ below:

PEP 204 – Range Literals

PEP 204 – Range Literals
Author:
Thomas Wouters <thomas at python.org>
Status:
Rejected
Type:
Standards Track
Created:
14-Jul-2000
Python-Version:
2.0
Post-History:
Table of Contents Introduction

This PEP describes the “range literal” proposal for Python 2.0. This PEP tracks the status and ownership of this feature, slated for introduction in Python 2.0. It contains a description of the feature and outlines changes necessary to support the feature. This PEP summarizes discussions held in mailing list forums, and provides URLs for further information, where appropriate. The CVS revision history of this file contains the definitive historical record.

List ranges

Ranges are sequences of numbers of a fixed stepping, often used in for-loops. The Python for-loop is designed to iterate over a sequence directly:

>>> l = ['a', 'b', 'c', 'd']
>>> for item in l:
...     print item
a
b
c
d

However, this solution is not always prudent. Firstly, problems arise when altering the sequence in the body of the for-loop, resulting in the for-loop skipping items. Secondly, it is not possible to iterate over, say, every second element of the sequence. And thirdly, it is sometimes necessary to process an element based on its index, which is not readily available in the above construct.

For these instances, and others where a range of numbers is desired, Python provides the range builtin function, which creates a list of numbers. The range function takes three arguments, start, end and step. start and step are optional, and default to 0 and 1, respectively.

The range function creates a list of numbers, starting at start, with a step of step, up to, but not including end, so that range(10) produces a list that has exactly 10 items, the numbers 0 through 9.

Using the range function, the above example would look like this:

>>> for i in range(len(l)):
...     print l[i]
a
b
c
d

Or, to start at the second element of l and processing only every second element from then on:

>>> for i in range(1, len(l), 2):
...     print l[i]
b
d

There are several disadvantages with this approach:

Slice Indices

In Python, a sequence can be indexed in one of two ways: retrieving a single item, or retrieving a range of items. Retrieving a range of items results in a new object of the same type as the original sequence, containing zero or more items from the original sequence. This is done using a “range notation”:

This range notation consists of zero, one or two indices separated by a colon. The first index is the start index, the second the end. When either is left out, they default to respectively the start and the end of the sequence.

There is also an extended range notation, which incorporates step as well. Though this notation is not currently supported by most builtin types, if it were, it would work as follows:

The third “argument” to the slice syntax is exactly the same as the step argument to range(). The underlying mechanisms of the standard, and these extended slices, are sufficiently different and inconsistent that many classes and extensions outside of mathematical packages do not implement support for the extended variant. While this should be resolved, it is beyond the scope of this PEP.

Extended slices do show, however, that there is already a perfectly valid and applicable syntax to denote ranges in a way that solve all of the earlier stated disadvantages of the use of the range() function:

The Proposed Solution

The proposed implementation of range-literals combines the syntax for list literals with the syntax for (extended) slices, to form range literals:

>>> [1:10]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [:5]
[0, 1, 2, 3, 4]
>>> [5:1:-1]
[5, 4, 3, 2]

There is one minor difference between range literals and the slice syntax: though it is possible to omit all of start, end and step in slices, it does not make sense to omit end in range literals. In slices, end would default to the end of the list, but this has no meaning in range literals.

Reference Implementation

The proposed implementation can be found on SourceForge [1]. It adds a new bytecode, BUILD_RANGE, that takes three arguments from the stack and builds a list on the bases of those. The list is pushed back on the stack.

The use of a new bytecode is necessary to be able to build ranges based on other calculations, whose outcome is not known at compile time.

The code introduces two new functions to listobject.c, which are currently hovering between private functions and full-fledged API calls.

PyList_FromRange() builds a list from start, end and step, returning NULL if an error occurs. Its prototype is:

PyObject * PyList_FromRange(long start, long end, long step)

PyList_GetLenOfRange() is a helper function used to determine the length of a range. Previously, it was a static function in bltinmodule.c, but is now necessary in both listobject.c and bltinmodule.c (for xrange). It is made non-static solely to avoid code duplication. Its prototype is:

long PyList_GetLenOfRange(long start, long end, long step)
Open issues Copyright

This document has been placed in the Public Domain.

References

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4