I'm reading 'Introducing Python' by Bill Lubanovic and it says that
Like zip(), range() returns an iterable object,
But isn't this an incorrect statement? For example,
s= zip([1,2,3],['one','two','three'])
next(s)
>> (1,'one')
next(s)
>> (2,'two')
a = range(10)
next(a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-54-7b3ad3809256> in <module>()
1 a = range(10)
----> 2 next(a)
TypeError: 'range' object is not an iterator
From the code above, we can check that zip() returns an iterator not an iterable object.
"an iterator" and "iterable" are not the same thing. All iterators are iterable, but not all iterables are iterators.
iterables can be passed to iter(), which returns an iterator. iterators can be passed to next(), and also can be passed to iter(), which acts as the identity function in that case.
https://docs.python.org/glossary.html#term-iterable
An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as
list,str, andtuple) and some non-sequence types likedict, file objects, and objects of any classes you define with an__iter__()or__getitem__()method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(),map(), …). When an iterable object is passed as an argument to the built-in functioniter(), it returns an iterator for the object. This iterator is good for one pass over the set of values. When using iterables, it is usually not necessary to calliter()or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop. See also iterator, sequence, and generator.
https://docs.python.org/glossary.html#term-iterator
An object representing a stream of data. Repeated calls to the iterator’s
__next__()method (or passing it to the built-in functionnext()) return successive items in the stream. When no more data are available aStopIterationexception is raised instead. At this point, the iterator object is exhausted and any further calls to its__next__()method just raiseStopIterationagain. Iterators are required to have an__iter__()method that returns the iterator object itself so every iterator is also iterable and may be used in most places where other iterables are accepted. One notable exception is code which attempts multiple iteration passes. A container object (such as a list) produces a fresh new iterator each time you pass it to theiter()function or use it in a for loop. Attempting this with an iterator will just return the same exhausted iterator object used in the previous iteration pass, making it appear like an empty container.
Iterators are iterable. The fact that zip returns an iterator doesn't mean it doesn't return an iterable. Also, if this book was written for Python 2, both zip and range returned lists in that version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With