efficiency of range() and xrange() in for loops

Alan Morgan amorgan at xenon.Stanford.EDU
Wed Apr 5 18:39:31 EDT 2006


In article <teXYf.69673$A83.1673324 at twister1.libero.it>,
Giovanni Bajo <noway at sorry.com> wrote:
>Steve R. Hastings wrote:
>
>>> in Python 2.X, range is defined to return a list.  if you start
>>> returning something else, you'll break stuff.
>>
>> Perhaps I'm mistaken here, but I don't see how this optimization could
>> possibly break anything.
>
>Because you assume that the only use-case of range() is within a for-loop.
>range() is a builtin function that can be used in any Python expression. For
>instance:
>
>RED, GREEN, BLUE, WHITE, BLACK = range(5)

Hmmm, this worked fine when I used xrange as well.  Am I missing something?
Obviously there *are* differences, viz:

a = range(5)
b = range(5)
a==b   # True!
c = xrange(5)
d = xrange(5)
c==d   # False!

and various other more arcane things, but do these actually happen
in real life?

Alan
-- 
Defendit numerus



More information about the Python-list mailing list