Join Jungwoo Ryoo for an in-depth discussion in this video Iterator example, part of Python: Design Patterns.
- Now let's define our own iterator. We start with creating our own list. As you can see here, the list we are using is a list of these German words, counting up to five. Here we'll be using a built in iterator provided by Python. The iterator will create a tuple, such as one and the German word, for example eins. So the iterator will return five tuples. So in this case, the tuple means a pair.
So one, eins, that's a tuple. Two, zwei, that's another tuple. So there will be five tuples returned in total. To make it happen, we need to add a line of code. We're using the built in iterator called zip, Z-I-P. And then, since we have to provide the number from one through five, that's why we are using this function, range, R-A-N-G-E.
And the argument of that function we're using is the number of words that will be provided as an argument of this function. So I'll be using count. And then the second argument of this function is the list itself. So numbers in German. The range argument here is important because it allows us to know up to what number we can count. The value returned by the iterator will be stored in a variable, in this case, called iterator.
Now using this iterator, we'll iterate through our iterable list, in this case numbers in German, and we'll extract the German numbers and put them in a generator called number. So that's exactly what this for loop is doing. And all we have to do now is simply sending our generator. To send a generator we use the keyword yield, Y-I-E-L-D.
And then the generator is called number. And the generator in this case contains numbers in German. Now it's time to test our code. Let's test the generator returned by our iterator, by using a for loop. So type for, placeholder, num. We'll be invoking our function count _to here, C-O-U-N-T underscore to, and then we'll have to decide, count to up to what number.
So in this case, I'll try to count up to three. And colon. So this function call, count_to(3) will return a generator we can use in this for loop. And then we use the print statement. Type print. And we'll format it. .format. And we'll be using the num variable, which is the German number that's going to be returned by the generator. And let's test the code.
Go ahead and go to Tools. Click on Build. So there's a typo. I'm missing an R. Iterator, R. Go to Tools, try to Build again. And it works. So we counted up to three. Eins, zwei, drei. But we can also try to count up to four. So let's try, for num in count_to(4).
And then a print statement. And let's try to run the code again. Go to Tools, Build. And this time, we count up to four. Eins, zwei, drei, vier. So the code works very well. The nice thing about Python is that it provides this built in iterator, and it makes our job much, much easier for us to develop our own customized iterator.
- Understanding design patterns
- Best design practices: consistency, completeness, and correctness
- Working with creational patterns
- Working with structural patterns
- Working with behavioral patterns
Skill Level Intermediate
Q: In the strategy pattern example, why does the code keep executing the default function rather than the alternate?
A: The programming demonstration skips the step of defining what the strategy pattern should be when an alternate function name is provided
as an argument as shown below.