[Tutor] Off Topic: xrange, WAS: Re: the and command

Eric Brunson brunson at brunson.com
Fri Aug 24 20:06:31 CEST 2007

Kent Johnson wrote:
> Dave Kuhlman wrote:
>> I thought xrange() returned an iterator.  I was wrong.
>> Actually xrange() returns an xrange object which, according to the
>> docs, is "an opaque sequence".
> Interesting. So an xrange object is an iterable sequence, not an iterator.

It's a fine distinction to the language lawyers.  Most of the time I see 
the term "iterator" used, the speaker is referring to an "iterable 
container", which is a container that defines an "__iter__()" method.  
That method returns the actual "iterator object", i.e. an object that 
implements an "__iter__()" and a "next()" method which returns the next 
item from the container until exhausted, then raises StopIteration.

So, xrange doesn't implement the next() method, but the object returned 
by xrange.__iter__() does, and *that* is the iterator object, though 
xrange can be referred to as "iterable".

Just semantics, but important when trying to be ultra-clear.  :-)


More information about the Tutor mailing list