On Fri, Aug 24, 2007 at 09:52:16AM -0400, Kent Johnson wrote:

Kent and Alan -

Thanks for encouraging me to do a little reading and experimenting.

I thought xrange() returned an iterator.  I was wrong.

Actually xrange() returns an xrange object which, according to the
docs, is "an opaque sequence".

Small points I suppose, but ...

There are some differences between an xrange object and an
iterator:

- an xrange object can be used multiple times to produce the
  sequence.  An iterator is exhausted after it has produced its
  sequence.  At least, thats what the docs seem to say, although, I
  suppose, it depends on the implementation of the individual
  iterator.  See http://docs.python.org/lib/typeiter.html.

- an xrange object is index-able; iterators are not.

Here is the relavant part from the docs:

  xrange([start,] stop[, step])
    This function is very similar to range(), but returns an
    `xrange object'' instead of a list. This is an opaque sequence
    type which yields the same values as the corresponding list,
    without actually storing them all simultaneously. The advantage
    of xrange() over range() is minimal (since xrange() still has
    to create the values when asked for them) except when a very
    large range is used on a memory-starved machine or when all of
    the range's elements are never used (such as when the loop is
    usually terminated with break).
        http://docs.python.org/lib/built-in-funcs.html#l2h-80

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to