Dumb python questions

Paul Rubin phr-n2001 at nightsong.com
Wed Aug 15 22:40:20 EDT 2001


Duncan Booth <duncan at NOSPAMrcp.co.uk> writes:
> Also, you say that xrange still eventually creates all of the numbers in 
> the range, but so does your while loop. Any loop eventually has to create 
> or access every index value that it uses. For a list of 100 numbers, it is 
> generally much faster for Python to create the list than it is for it to 
> create each of the numbers by adding 1 to the preceding value.

Yes, I see in

  http://www.python.org/doc/current/lib/typesseq-xrange.html

that the xrange object uses a constant amount of storage.  In another
description that I saw somewhere, it sounded as if xrange(n) ended
up allocating the full sequence of size n if you iterated through it.

Xrange is apparently a primitive type, which sounds like a big kludge.
Maybe in Python 2.2 they can make it into an iterator.  That would be
the right way to deal with this problem.



More information about the Python-list mailing list