need help on need help on generator...

Alex Martelli aleaxit at yahoo.com
Sat Jan 22 10:20:36 CET 2005


Nick Coghlan <ncoghlan at iinet.net.au> 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)
>>> xx.next()
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'...).


Alex



More information about the Python-list mailing list