PEP 234: Iterators

Just van Rossum just at letterror.com
Sat May 5 14:55:31 EDT 2001


Bernhard Herzog wrote:
> 
> Just van Rossum <just at letterror.com> writes:
> 
> > the xrange object supports random access, whereas an iterator does
> > not.
> 
> Well, I see no reason why an iterator object couldn't provide random
> access in addition to the iterator interface.

There's no reason it couldn't, but why would you? Iterators have state,
so generally shouldn't be shared (eg. across threads). If xrange objects
were iterators, you couldn't do this (or at least it would have a
different effect than it has now ;-), even if they *also* provided
random access:

  r = xrange(10)
  for x in r:
      for y in r:
          ...

Just



More information about the Python-list mailing list