Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 260 – Simplify xrange()

Author:
Guido van Rossum <guido at python.org>
Status:
Final
Type:
Standards Track
Created:
26-Jun-2001
Python-Version:
2.2
Post-History:
26-Jun-2001

Table of Contents

Abstract

This PEP proposes to strip thexrange() object from some rarelyused behavior likex[i:j] andx*n.

Problem

Thexrange() function has one idiomatic use:

foriinxrange(...):...

However, thexrange() object has a bunch of rarely used behaviorsthat attempt to make it more sequence-like. These are so rarelyused that historically they have has serious bugs (e.g. off-by-oneerrors) that went undetected for several releases.

I claim that it’s better to drop these unused features. This willsimplify the implementation, testing, and documentation, andreduce maintenance and code size.

Proposed Solution

I propose to strip thexrange() object to the bare minimum. Theonly retained sequence behaviors arex[i],len(x), andrepr(x).In particular, these behaviors will be dropped:

  • x[i:j] (slicing)
  • x*n,n*x (sequence-repeat)
  • cmp(x1,x2) (comparisons)
  • iinx (containment test)
  • x.tolist() method
  • x.start,x.stop,x.step attributes

I also propose to change the signature of thePyRange_New() C APIto remove the 4th argument (the repetition count).

By implementing a custom iterator type, we could speed up thecommon use, but this is optional (the default sequence iteratordoes just fine).

Scope

This PEP affects thexrange() built-in function and thePyRange_New() C API.

Risks

Somebody’s code could be relying on the extended code, and thiscode would break. However, given that historically bugs in theextended code have gone undetected for so long, it’s unlikely thatmuch code is affected.

Transition

For backwards compatibility, the existing functionality will stillbe present in Python 2.2, but will trigger a warning. A yearafter Python 2.2 final is released (probably in 2.4) thefunctionality will be ripped out.

Copyright

This document has been placed in the public domain.


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

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


[8]ページ先頭

©2009-2025 Movatter.jp