@@ -105,9 +105,8 @@ this:
105
105
106
106
## Iterating manually
107
107
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.
111
110
112
111
``` python
113
112
>> > e = enumerate (' abc' )
@@ -146,9 +145,11 @@ StopIteration
146
145
```
147
146
148
147
There 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...
152
153
153
154
``` python
154
155
for pair in enumerate (' hello' ):
@@ -170,7 +171,7 @@ while True:
170
171
```
171
172
172
173
The 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 .
174
175
175
176
## Converting to iterators
176
177
@@ -282,13 +283,15 @@ Let's try out our thingy function and see how it works.
282
283
```
283
284
284
285
What 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.
288
291
289
292
![ Generators.] ( ../images/generators.png )
290
293
291
- The generator we got works just like other iterators:
294
+ The generators that thingy gives us work just like other iterators:
292
295
293
296
``` python
294
297
>> > t = thingy()
@@ -373,10 +376,10 @@ Here's a drawing of what's going on:
373
376
374
377
![ A picture of printygen.] ( ../images/freeze-melt.png )
375
378
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.
380
383
381
384
` yield ` is useful when we want the function to output so many things
382
385
that making a list of them would be too slow or the list wouldn't fit in
@@ -426,20 +429,19 @@ number of things:
426
429
```
427
430
428
431
[ 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() ` .
431
434
432
435
## Summary
433
436
434
437
- An iterable is something that we can for loop over.
435
438
- An iterator is an iterable that remembers its position.
436
439
- For loops create an iterator of the iterable and call its ` __next__ `
437
440
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.
443
445
444
446
***
445
447
0 commit comments