Chapter 5. Iterators and Generators
When many people with experience in another language start learning Python, they are taken
aback by the difference in for
loop notation. That is to say, instead of
writing
#
Other languages
for
(
i
=
0
;
i
<
N
;
i
+
+
)
{
do_work
(
i
)
;
}
they are introduced to a new function called range
:
# Python
for
i
in
range
(
N
)
:
do_work
(
i
)
*TODO for Micha from Ian, a reader filed errata noting that range is not a generator but a range type https://docs.python.org/3/library/stdtypes.html#typesseq-range and suggests talking of laziness instead, they quoted https://stackoverflow.com/questions/13092267/if-range-is-a-generator-in-python-3-3-why-can-i-not-call-next-on-a-range ...
Get High Performance Python, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.