Course 2.2Lesson 3 of 5
A for loop works in two steps. It asks the iterable for an iterator with iter, and then calls next on the iterator again and again until a StopIteration is raised. An iterable can give an iterator, such as a list, a text, a dictionary, a range or a file. An iterator remembers where it is and can be used only once, while a list can be looped over many times. To make your own iterator, write a class with the methods __iter__, which returns the object, and __next__, which gives the next value or raises StopIteration. Understanding this explains why a generator or a file can be looped over only once, and why range uses so little memory.
Try the code and the simulations beside the lesson.
After this lesson you can
for loop does with iter and next,for loop, andYou have written for n in numbers: hundreds of times. But how does Python go from the list to the items, one at a time? It does two things, and you can do them by hand.
Predict first. What does the second next give, and what happens at the fourth? Then run it.
iter(numbers) asks the list for an iterator: a small object that remembers where it is in the list.next(it) asks the iterator for the next value. The first call gives 10, the second 20, the third 30.StopIteration. This is not an error in the usual sense. It is the signal that the loop is over.The for loop does exactly this: it calls iter once, then next again and again, and catches StopIteration to end quietly. The try and except in the program shows what the loop hides.
Two words, and the difference matters.
| Iterable | Iterator | |
|---|---|---|
| What it is | something that you can loop over | the thing that does the looping |
| How you get it | it is the list, text, dictionary, range, file | iter(iterable) |
| What it can do | give an iterator with iter | give the next value with next |
| How often | as many times as you like | once: it is used up |
| Examples | list, tuple, str, dict, set, range, file | the result of iter(...), a generator |
The program shows the difference. list(it) gives all the items the first time, and an empty list the second time: the iterator is used up. But list(numbers) gives the items again and again, because each call gets a fresh iterator from the list.
This matters in real programs. If you loop over an iterator once, and then try again, you get nothing, and no error. A file object and a generator behave this way too.
To make your own kind of thing that works in a for loop, write a class with two special methods.
This is a lot of machinery for a countdown. In the next lesson, you will see that generators do the same job in a few lines. But understanding the machinery shows you what a generator is.
An iterator can only go forward. Once it is used up, list(it) gives an empty list: start a new iterator to go again.
A list holds all its items in memory at once. A range(10 ** 9) does not: it only remembers the start, the stop and the step, and makes each number when asked. We say that range is lazy. An iterator is often lazy: it makes a value only when it is asked for one, and then forgets it.
Why does that matter? A list of a billion numbers needs gigabytes of memory, and a computer would run out. A lazy iterator needs a few bytes, and gives its values one at a time. For data that is big, or endless, lazy is the only way.
raise StopIteration in __next__. The loop never ends.__next__. The same value for ever.__next__ without __iter__. The for loop first asks for iter.next on a list. A list is an iterable, not an iterator. Use next(iter(my_list)).Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
__iter__ and __next__, and the checker uses it in loops.for loop calls iter once, and next again and again, until StopIteration.__iter__ and __next__ can be used in for, list and sum.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
next(it) do when the iterator has no more values?list(it) give if you call it twice on the same iterator?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.