@@ -105,9 +105,8 @@ this:
105105
106106## Iterating manually
107107
108- Iterators have a magic method called ` __next__ ` , and there's a built-in
109- function called ` next() ` for calling that. Calling ` next() ` on an
110- iterator gets the next value and moves it forward. Like this:
108+ Iterators have a magic method called ` __next__ ` that gets next value and
109+ moves the iterator forward.
111110
112111``` python
113112>> > e = enumerate (' abc' )
@@ -146,9 +145,11 @@ StopIteration
146145```
147146
148147There is usually not a good way to check if the iterator is at the end,
149- and it's best to just try to get a value from it and catch
150- StopIteration. That's actually what for looping over an iterator does.
151- For example, this code...
148+ and it's best to just try to get a value from it and
149+ [ catch] ( ../basics/exceptions.md#catching-exceptions ) StopIteration.
150+
151+ That's actually what for loops do. For example,
152+ this code...
152153
153154``` python
154155for pair in enumerate (' hello' ):
@@ -170,7 +171,7 @@ while True:
170171```
171172
172173The for loop version is much simpler to write and I wrote the while loop
173- version just to explain what the for loop does .
174+ version just to show how the for loop works .
174175
175176## Converting to iterators
176177
@@ -282,13 +283,15 @@ Let's try out our thingy function and see how it works.
282283```
283284
284285What the heck? We don't return anything from the function, but it still
285- doesn't return None! Putting a ` yield ` anywhere in a function makes it
286- return ** generators** instead of returning anything. ** Generators are
287- iterators** with some more features that we don't need to care about.
286+ doesn't return None!
287+
288+ Putting a ` yield ` anywhere in a function makes it return ** generators** .
289+ ** Generators are iterators** with some more features that we don't need
290+ to care about.
288291
289292![ Generators.] ( ../images/generators.png )
290293
291- The generator we got works just like other iterators:
294+ The generators that thingy gives us work just like other iterators:
292295
293296``` python
294297>> > t = thingy()
@@ -373,10 +376,10 @@ Here's a drawing of what's going on:
373376
374377![ A picture of printygen.] ( ../images/freeze-melt.png )
375378
376- The good news is that ** usually we don't need to worry about when the
377- parts between the yields run** . Actually we don't even need to use
378- ` iter() ` or ` next() ` most of the time, but I think it's nice to know how
379- for loops work.
379+ The good news is that ** usually we don't need to worry about when
380+ exactly the parts between the yields run** . Actually we don't even need
381+ to use ` iter() ` and ` next() ` most of the time, but I think it's nice to
382+ know how for loops work.
380383
381384` yield ` is useful when we want the function to output so many things
382385that making a list of them would be too slow or the list wouldn't fit in
@@ -426,20 +429,19 @@ number of things:
426429```
427430
428431[ The itertools module] ( https://docs.python.org/3/library/itertools.html )
429- contains many useful functions like this. For example,
430- ` itertools.count(1) ` does the same thing as our ` count() ` .
432+ contains many useful things like this. For example, ` itertools.count(1) `
433+ does the same thing as our ` count() ` .
431434
432435## Summary
433436
434437- An iterable is something that we can for loop over.
435438- An iterator is an iterable that remembers its position.
436439- For loops create an iterator of the iterable and call its ` __next__ `
437440 method until it raises a StopIteration.
438- - Functions that contain yields return generators. Iterating the
439- generator runs the function bit by bit and gives us the values it
440- yields.
441- - The itertools module contains many useful functions that return
442- iterators.
441+ - Functions that contain yields return generators. Calling ` next() ` on a
442+ generator runs it to the next yield and gives us the value it yielded.
443+ - [ The itertools module] ( https://docs.python.org/3/library/itertools.html )
444+ contains many useful iterator-related things.
443445
444446***
445447
0 commit comments