Why isn't range getting exhausted in Python-3? -
if following:
a = range(9) in a: print(i) # must exhausted in a: print(i) # starts over!
python's generators, after raising stopiteration
, stops looping. how range producing pattern - restarts after every iteration.
as has been stated others, range
not generator, sequence type (like list) makes iterable
not same iterator
.
the differences between iterable
, iterator
, generator
subtle (at least new python).
- an
iterator
provides__next__
method , can exhausted, raisingstopiteration
. - an
iterable
object providesiterator
on content. whenever__iter__
method called returns new iterator object, can (indirectly) iterate on multiple times. a
generator
function returnsiterator
, of cause can exhausted.also know is,
for
loop automaticly queriesiterator
ofiterable
. why can writefor x in iterable: pass
instead offor x in iterable.__iter__(): pass
orfor x in iter(iterable): pass
.
all of in documentation, imho difficult find. best starting point glossary.
Comments
Post a Comment