Strange range

Marko Rauhamaa marko at pacujo.net
Fri Apr 1 10:32:35 EDT 2016


Fabien <fabien.maussion at gmail.com>:

> On 04/01/2016 03:26 PM, Steven D'Aprano wrote:
>> Incorrect. range is a lazy sequence.
>
> But how does range "know" that it has to start from scratch again?

The "for .. in" statement invokes the __iter__() method of the iterable
object to get an iterator (maybe via the builtin iter() function).

Iterators have an __iter__() method as well which returns the iterator
itself.

Thus,

   >>> it = iter(range(10))
   >>> it is iter(it)
   True


Marko



More information about the Python-list mailing list