xrange question

John Flynn transpicio at yahoo.com.au
Sun May 6 13:14:54 EDT 2001


I'm puzzled about the purpose of xrange.

My understanding is that it provides a way to iterate over a range of
numbers without allocating memory for a large list of numbers in advance,
ie. it generates values on the fly as needed. I can see how this might be
useful for looping over very large sequences, but two things puzzle me.

Firstly, I can't see how 'xrange' does anything that can't be done more
efficiently with 'while'. I think xrange must have a purpose I'm not seeing.

Secondly, it seems that the size of a sequence generated by xrange is
limited to INT_MAX. Is there a good reason for this? I would have thought
that, if 'xrange' exists to cut the cost of manipulating huge sequences, it
would make sense to allow xranges of arbitrary length. Otherwise I don't see
what they buy me.

The question is: what am I missing? What does 'xrange' do that 'while'
cannot do better?






More information about the Python-list mailing list