Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 281 – Loop Counter Iteration with range and xrange

PEP 281 – Loop Counter Iteration with range and xrange

Author:
Magnus Lie Hetland <magnus at hetland.org>
Status:
Rejected
Type:
Standards Track
Created:
11-Feb-2002
Python-Version:
2.3
Post-History:


Table of Contents

Abstract

This PEP describes yet another way of exposing the loop counter infor-loops. It basically proposes that the functionality of thefunctionindices() fromPEP 212 be included in the existingfunctionsrange() andxrange().

Pronouncement

In commenting onPEP 279’senumerate() function, this PEP’s authoroffered, “I’m quite happy to have it makePEP 281 obsolete.”Subsequently,PEP 279 was accepted into Python 2.3.

On 17 June 2005, the BDFL concurred with it being obsolete andhereby rejected the PEP. For the record, he found some of theexamples to somewhat jarring in appearance:

>>>range(range(5),range(10),range(2))[5, 7, 9]

Motivation

It is often desirable to loop over the indices of a sequence. PEP212 describes several ways of doing this, including adding abuilt-in function called indices, conceptually defined as:

defindices(sequence):returnrange(len(sequence))

On the assumption that adding functionality to an existing built-infunction may be less intrusive than adding a new built-in function,this PEP proposes adding this functionality to the existingfunctionsrange() andxrange().

Specification

It is proposed that all three arguments to the built-in functionsrange() andxrange() are allowed to be objects with a length(i.e. objects implementing the__len__ method). If an argumentcannot be interpreted as an integer (i.e. it has no__int__method), its length will be used instead.

Examples:

>>>range(range(10))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>>range(range(5),range(10))[5, 6, 7, 8, 9]>>>range(range(5),range(10),range(2))[5, 7, 9]>>>list(xrange(range(10)))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>>list(xrange(xrange(10)))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]# Number the lines of a file:lines = file.readlines()for num in range(lines):    print num, lines[num]

Alternatives

A natural alternative to the above specification is allowingxrange() to access its arguments in a lazy manner. Thus, insteadof using their length explicitly,xrange can return one index foreach element of the stop argument until the end is reached. Asimilar lazy treatment makes little sense for the start and steparguments since their length must be calculated before iterationcan begin. (Actually, the length of the step argument isn’t neededuntil the second element is returned.)

A pseudo-implementation (using only the stop argument, and assumingthat it is iterable) is:

defxrange(stop):i=0forxinstop:yieldii+=1

Testing whether to useint() or lazy iteration could be done bychecking for an__iter__ attribute. (This example assumes thepresence of generators, but could easily have been implemented as aplain iterator object.)

It may be questionable whether this feature is truly useful, sinceone would not be able to access the elements of the iterable objectinside the for loop through indexing.

Example:

# Printing the numbers of the lines of a file:fornuminrange(file):printnum# The line itself is not accessible

A more controversial alternative (to deal with this) would be toletrange() behave like the functionirange() ofPEP 212 whensupplied with a sequence.

Example:

>>>range(5)[0, 1, 2, 3, 4]>>>range('abcde')[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]

Backwards Compatibility

The proposal could cause backwards incompatibilities if argumentsare used which implement both__int__ and__len__ (or__iter__ inthe case of lazy iteration withxrange). The author does notbelieve that this is a significant problem.

Copyright

This document has been placed in the public domain.


Source:https://github.com/python/peps/blob/main/peps/pep-0281.rst

Last modified:2025-02-01 08:55:40 GMT


[8]ページ先頭

©2009-2026 Movatter.jp