1. 28

  2. 19

    I love this caveat:

    I’d like to ask that you do not use the information below as an excuse to be unkind to anyone, whether new learners or experienced Python programmers.

    1. 9

      Lists are not iterators either. I don’t understand why it is surprising.

      Yes, “iterables” vs “iterators” terminology is confusing, but it’s obvious that iterator is a mutable stateful object and lists, ranges, etc don’t have this state right inside of them.

      1. 8

        I think of it this way: An iterable is the equivalent of a file that you can ‘cat’ and consume the output line by line in a typical unix pipeline. On the other, iterator is the equivalent of a file socket. You read from it, and the data is consumed – never to return.

        1. 6

          Hopefully this won’t come out as unkind, but the article does seem like an overly long way of saying that “iterable” is what produce “iterators” which you can only walk once. And it’s not actually specific to range(), so why put it prominently in the title?

          1. 4

            Most probably the idea for the article itself came from someone calling range() and iterator, but I get your point and must agree on how the topic over-extended which in the case of a beginner is even more confusing.

            1. 3

              I liked the section on how range objects are more like lazy collections than lazy streams (in that they support containment queries, lengths, etc without consuming the range). Agreed that that was not the majority of the article, but it’s still an interesting little morsel.

            2. 4

              This is great. The succinct demonstration (from the article) is this one:

              >>> numbers = range(3)
              >>> tuple(numbers)
              (0, 1, 2)
              >>> tuple(numbers)
              (0, 1, 2)
              1. 2

                I’m not sure why this is surprising? range has always been a sequence generating function. The best example is range in Python 2, which produces a list. The fact that range (py3) and xrange (py2) generate, lazily, a list and memoize it is no different, but simply an implementation detail.

                But, obviously this was written because there is confusion around it…. so, ¯_(ツ)_/¯