l = range(int(1E9))

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun May 3 07:21:58 EDT 2015


On Sun, 3 May 2015 02:51 pm, Ian Kelly wrote:

> On Sat, May 2, 2015 at 5:51 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>> On 5/2/2015 5:31 PM, Ian Kelly wrote:
>>
>>> Would it have been better if range() had been implemented as xrange()
>>> from the beginning? Sure, that would have been great. Except for one
>>> small detail: the iterator protocol didn't exist back then.
>>
>>
>> For loops originally used the getitem iterator protocol. xrange objects
>> have
>> a __getitem__ method, but not __iter__ or __next__.  As Mark pointed out,
>> they were introduced in 1993.
> 
> I'm aware of getitem iterators; just didn't realize that xrange used
> it or was that old.


Yep, xrange objects are *not* iterators:

py> r = xrange(100)
py> iter(r) is r
False
py> next(r)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: xrange object is not an iterator



-- 
Steven




More information about the Python-list mailing list