need help on need help on generator...

Alex Martelli aleaxit at
Sat Jan 22 04:20:36 EST 2005

Nick Coghlan <ncoghlan at> wrote:

> 5. Several builtin functions return iterators rather than lists, specifically
> xrange(), enumerate() and reversed(). Other builtins that yield sequences
> (range(), sorted(), zip()) return lists.

Yes for enumerate and reversed, no for xrange:

>>> xx=xrange(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'xrange' object has no attribute 'next'

it SHOULD return an iterator, no doubt, but it doesn't (can't, for
backwards compatibility reasons).  Neither does it return a list: it
returns "an `xrange' object", a specialized type that's not an iterator,
though it's iterable.  It's a type, btw:

>>> xrange
<type 'xrange'>

so it's not surprising that calling it returns instances of it
(enumerate and reversed are also types, but *WITH* 'next'...).


More information about the Python-list mailing list