1
00:00:00,000 --> 00:00:04,990
Generator functions are an incredibly useful pattern in Python.

2
00:00:05,000 --> 00:00:08,990
What a generator function does is it actually creates an iterator.

3
00:00:09,000 --> 00:00:11,990
Let's take a look at how this works.

4
00:00:12,000 --> 00:00:14,990
We can make a working copy of generator.py.

5
00:00:15,000 --> 00:00:20,990
I'm going to call it generator-working.py.

6
00:00:21,000 --> 00:00:24,990
Go ahead and open that up and take a look.

7
00:00:25,000 --> 00:00:25,990
This is the generator here.

8
00:00:26,000 --> 00:00:29,990
Now it calls this isprime, which is very similar to the one that we used in

9
00:00:30,000 --> 00:00:33,990
our functions example, except instead of the Print statements, it returns

10
00:00:34,000 --> 00:00:37,990
False if it's not prime, and it returns True if it is prime. So that's a

11
00:00:38,000 --> 00:00:39,990
little utility function.

12
00:00:40,000 --> 00:00:44,990
This one here, it looks, at first glance, like a normal function, and in fact, it

13
00:00:45,000 --> 00:00:49,990
is, except it has this yield statement.

14
00:00:50,000 --> 00:00:50,990
Yield is like return.

15
00:00:51,000 --> 00:00:52,990
It returns a value.

16
00:00:53,000 --> 00:00:58,990
You see here we have return False, return True. Those return those Boolean values.

17
00:00:59,000 --> 00:01:00,990
This one here returns a number,

18
00:01:01,000 --> 00:01:06,990
and it returns this number only if isprime returns true, so it returns the next

19
00:01:07,000 --> 00:01:11,990
prime number, but it uses yield instead of return.

20
00:01:12,000 --> 00:01:15,990
And what yield does is it returns a value, but then the next time the function

21
00:01:16,000 --> 00:01:19,990
is called, it continues execution after the yield.

22
00:01:20,000 --> 00:01:22,990
So in this case, we have this while loop.

23
00:01:23,000 --> 00:01:27,990
And it checks to see if the number is prime.

24
00:01:28,000 --> 00:01:30,990
If it's not, it increments and checks the next one.

25
00:01:31,000 --> 00:01:33,990
And if it is, it yields.

26
00:01:34,000 --> 00:01:35,990
And that will return a value.

27
00:01:36,000 --> 00:01:38,990
And then the next time this function is called, it'll just continue here,

28
00:01:39,000 --> 00:01:41,990
incrementing and looking for the next one.

29
00:01:42,000 --> 00:01:46,990
And so this actually, because of these of yield, it returns an iterator object,

30
00:01:47,000 --> 00:01:49,990
that is suitable for use in a for loop.

31
00:01:50,000 --> 00:01:53,990
And here we have a for loop, and for n in primes,

32
00:01:54,000 --> 00:01:55,990
and so it calls this primes function.

33
00:01:56,000 --> 00:01:57,990
There is the primes function.

34
00:01:58,000 --> 00:02:03,990
And for each iteration, it'll put the value in n. And it'll print it out, and it

35
00:02:04,000 --> 00:02:07,990
tests to see if it's greater than 100. Then it breaks out of the loop.

36
00:02:08,000 --> 00:02:12,990
And so this uses this generator function as an iterator.

37
00:02:13,000 --> 00:02:14,990
And it will print a list of prime numbers.

38
00:02:15,000 --> 00:02:20,990
Let's go ahead and take a look at this as it works. We'll run it.

39
00:02:21,000 --> 00:02:28,990
And here we have a complete list of all the prime numbers up to 97.

40
00:02:29,000 --> 00:02:31,990
So you can see that this is incredibly useful.

41
00:02:32,000 --> 00:02:35,990
And we'll some more examples of it as we go through the course.

42
00:02:36,000 --> 00:02:39,990
So this is a generator function, and what it generates is an iterator that's

43
00:02:40,000 --> 00:02:50,000
suitable for use in a for loop, just like any iterator in the Python language.

