Strange range
Marko Rauhamaa
marko at pacujo.net
Sat Apr 2 16:44:16 EDT 2016
Ned Batchelder <ned at nedbatchelder.com>:
> This analogy illuminates an important point: a single iterable can have
> a number of active iterators working over it at once, just as a book can
> have a number of bookmarks in it at once.
>
> nums = [1, 2, 3]
> for i in nums:
> for j in nums:
> print i, j
>
> This prints all the pairs of numbers, because the iterator in the first
> loop is independent of the iterator(s) in the second loop, even though
> they are iterating over the same iterator (the nums list). Without the
> extra indirection of iterators over iterables, this code would get
> tangled up.
I don't have a problem with a list being a "reiterable." I only was
surprised about range(), which I had thought to be a plain,
down-to-earth iterator. There's barely any other practical use for a
range, I believe.
Marko
More information about the Python-list
mailing list