Strange range

Erik python at lucidity.plus.com
Fri Apr 1 15:58:33 EDT 2016


On 01/04/16 15:34, Marko Rauhamaa wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> *A range object is not an iterator.*
>
> We now have learned as much.
>
> However, doesn't that extra level of indirection seem like an odd
> choice?

If you write your own class which has an __iter__ method, would you expect:

   >>> o = MyClass()
   >>> i1 = iter(o)
   >>> i2 = iter(o)
   >>> list(i1)
   ['foo', 'bar', 'baz']
   >>> list(i2)
   []

?

Or, would you expect both iterators to be independent and both return 
the 'foo', 'bar', 'baz' sequence (where that is the hypothetical result 
of iterating over your object)?

If you now replace MyClass() with range(10), why would you expect the 
two iterators to be related?

E.



More information about the Python-list mailing list