PEP 260: simplify xrange()

Delaney, Timothy tdelaney at avaya.com
Tue Jun 26 20:36:25 EDT 2001


> Here's another sweet and short PEP.  What do folks think?  Is
> xrange()'s complexity really worth having?
> 
> --Guido van Rossum (home page: http://www.python.org/~guido/)
> 
> PEP: 260
> Title: Simplify xrange()
> Version: $Revision: 1.1 $
> Author: guido at python.org (Guido van Rossum)
> Status: Draft
> Type: Standards Track
> Python-Version: 2.2
> Created: 26-Jun-2001
> Post-History: 26-Jun-2001
> 
> Abstract
> 
>     This PEP proposes to strip the xrange() object from some rarely
>     used behavior like x[i:j] and x*n.
> 
> 
> Problem
> 
>     The xrange() function has one idiomatic use:
> 
>         for i in xrange(...): ...

If this is to be done, I would also propose that xrange() and range() be
changed to allow passing in a straight-out sequence such as in the following
code in order to get rid of the need for range(len(seq)):

import __builtin__

def range (start, stop=None, step=1, range=range):
    """"""

    start2 = start
    stop2 = stop

    if stop is None:
        stop2 = start
        start2 = 0

    try:
        return range(start2, stop2, step)
    except TypeError:
        assert stop is None
        return range(len(start))

def xrange (start, stop=None, step=1, xrange=xrange):
    """"""

    start2 = start
    stop2 = stop

    if stop is None:
        stop2 = start
        start2 = 0

    try:
        return xrange(start2, stop2, step)
    except TypeError:
        assert stop is None
        return xrange(len(start))

a = [5, 'a', 'Hello, world!']
b = range(a)
c = xrange(4, 6)
d = xrange(b)
e = range(c)

print a
print b
print c
print d
print e
print range(d, 2)

Tim Delaney




More information about the Python-list mailing list