This PEP proposes to strip thexrange() object from some rarelyused behavior likex[i:j] andx*n.
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.
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() methodx.start,x.stop,x.step attributesI 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).
This PEP affects thexrange() built-in function and thePyRange_New() C API.
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.
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.
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