WEBVTT 1 00:00:00.180 --> 00:00:01.400 Hi and welcome back. 2 00:00:01.400 --> 00:00:03.010 In this video, I wanted to show you 3 00:00:03.010 --> 00:00:07.180 how to yield from another iterator in Python. 4 00:00:08.430 --> 00:00:10.380 So let's look at some code, I think that's gonna be 5 00:00:10.380 --> 00:00:12.680 the easiest way to understand what's going on. 6 00:00:14.020 --> 00:00:16.307 From collections, I'm gonna import the deque 7 00:00:16.307 --> 00:00:19.225 and then we're gonna create a friends deque 8 00:00:19.225 --> 00:00:23.600 and that is going to take in Rolf, Jose, 9 00:00:23.600 --> 00:00:26.629 Charlie, Jen and Anna 10 00:00:26.629 --> 00:00:30.110 and then we're gonna have a get_friend function, 11 00:00:30.110 --> 00:00:32.980 that just does yield from friends, 12 00:00:34.060 --> 00:00:38.560 okay, if we then call sort of c is get_friend 13 00:00:38.560 --> 00:00:42.196 and we do next of c, next of c, 14 00:00:42.196 --> 00:00:44.900 what we're gonna get, if we run this file, 15 00:00:47.200 --> 00:00:49.360 oh, of course we have to print, 16 00:00:49.360 --> 00:00:51.320 that is always helpful 17 00:00:55.690 --> 00:00:58.122 is Rolf and Jose, so this yield from, 18 00:00:58.122 --> 00:01:01.210 you can think of it for now, not forever, 19 00:01:01.210 --> 00:01:02.107 we're gonna change this soon, 20 00:01:02.107 --> 00:01:04.090 you can think of it for now 21 00:01:04.090 --> 00:01:07.163 as just getting the next value of another thing, 22 00:01:07.163 --> 00:01:09.708 that you can get a value from, 23 00:01:09.708 --> 00:01:12.886 so what it's getting here is the first element of this deque 24 00:01:12.886 --> 00:01:14.327 and then when you call next again, 25 00:01:14.327 --> 00:01:16.428 it's getting the next element of this deque, 26 00:01:16.428 --> 00:01:20.060 it allows you to sort of yield 27 00:01:20.060 --> 00:01:24.070 from existing iterators, okay, 28 00:01:25.420 --> 00:01:29.930 essentially this has now become a generator, 29 00:01:29.930 --> 00:01:34.199 that gives you each value of a list or a deque in this case, 30 00:01:34.199 --> 00:01:36.940 it's a good way of creating a generator of something, 31 00:01:36.940 --> 00:01:40.852 instead of doing, you know, x for x in friends, 32 00:01:40.852 --> 00:01:43.550 which would be another way of doing this. 33 00:01:46.100 --> 00:01:49.193 Let's use this generator now in another function, 34 00:01:49.193 --> 00:01:53.743 so what we're gonna do is define a greet function, 35 00:01:53.743 --> 00:01:56.340 that is going to take in a generator 36 00:01:57.420 --> 00:01:59.922 and it's just going to run an infinite loop 37 00:01:59.922 --> 00:02:04.100 and it's gonna try to get the next value of that generator 38 00:02:04.100 --> 00:02:08.100 and it's gonna yield HELLO friend, 39 00:02:09.260 --> 00:02:12.400 if it fails to do that, because there's a StopIteration, 40 00:02:13.860 --> 00:02:16.760 then it's just gonna pass and it's gonna do nothing, okay. 41 00:02:18.340 --> 00:02:20.790 Now this may not make much sense, 42 00:02:20.790 --> 00:02:22.747 until we start sort of using this. 43 00:02:22.747 --> 00:02:25.440 I'm gonna minimise that just so we get a bit more space. 44 00:02:25.440 --> 00:02:29.397 What we're gonna do is gonna get a friend's generator 45 00:02:29.397 --> 00:02:31.810 and that's gonna be our get_friend 46 00:02:32.910 --> 00:02:35.592 and then we're gonna create another generator, 47 00:02:35.592 --> 00:02:38.320 which is gonna be our greet generator 48 00:02:38.320 --> 00:02:42.230 and that's gonna get our friend's generator passed into it, 49 00:02:43.950 --> 00:02:46.350 okay, hopefully this makes sense. 50 00:02:46.350 --> 00:02:49.480 Our generator here, we can call next on it 51 00:02:49.480 --> 00:02:51.336 and it gives us Rolf, then we can call next on it 52 00:02:51.336 --> 00:02:53.430 and it gives us Jose, and so forth, 53 00:02:54.590 --> 00:02:57.370 we're giving that generator to our greet function 54 00:02:58.660 --> 00:03:01.970 and what that does is it goes over a whole for loop 55 00:03:03.270 --> 00:03:05.800 and it's gonna try to get the next value 56 00:03:05.800 --> 00:03:09.576 of the generator we passed in, that's our friends generator, 57 00:03:09.576 --> 00:03:11.955 that's gonna be Rolf, the first time 58 00:03:11.955 --> 00:03:16.790 and it's gonna yield back to us this string, HELLO Rolf, 59 00:03:18.600 --> 00:03:20.410 if it cannot get the next value, 60 00:03:20.410 --> 00:03:23.690 because it's run out of values in this deque, 61 00:03:23.690 --> 00:03:25.470 then it's going to just do nothing 62 00:03:25.470 --> 00:03:27.660 and we're going to end this loop, 63 00:03:28.920 --> 00:03:31.760 okay, it's gonna raise a StopIteration there, 64 00:03:33.530 --> 00:03:36.160 let's have a wee look at how this would go. 65 00:03:44.700 --> 00:03:47.130 Again, all that's happening here 66 00:03:47.130 --> 00:03:52.130 is we are calling the next of this greet generator, 67 00:03:52.590 --> 00:03:54.010 which is going inside this loop 68 00:03:54.010 --> 00:03:58.750 and it's gonna move over to this yield, 69 00:03:58.750 --> 00:04:02.636 it's gonna yield HELLO Rolf and then it's gonna suspend, 70 00:04:02.636 --> 00:04:05.978 when we press next of g again, it's gonna proceed, 71 00:04:05.978 --> 00:04:08.350 it's gonna go back into the top of the loop, 72 00:04:08.350 --> 00:04:10.639 it's gonna get the next value of the friend 73 00:04:10.639 --> 00:04:14.270 and it's gonna yield HELLO Jose, 74 00:04:15.700 --> 00:04:18.048 okay, so the fact that this is a while loop 75 00:04:18.048 --> 00:04:20.990 doesn't mean it's gonna run continuously, 76 00:04:20.990 --> 00:04:21.897 because we've got the yield, 77 00:04:21.897 --> 00:04:25.810 so the function is gonna suspend every time we call next. 78 00:04:25.810 --> 00:04:27.190 Let's run this file again 79 00:04:28.444 --> 00:04:31.530 and you can see now that we get sort of what you'd expect, 80 00:04:31.530 --> 00:04:33.710 HELLO Rolf and HELLO Jose. 81 00:04:35.350 --> 00:04:39.320 What we've got here is some pretty awesome thing, 82 00:04:39.320 --> 00:04:40.930 even if it's not so clear yet, 83 00:04:40.930 --> 00:04:45.930 because when we call next on this top level function, 84 00:04:46.380 --> 00:04:50.190 that is then running this function up to the first element 85 00:04:51.630 --> 00:04:54.103 and then both of these suspend, 86 00:04:54.103 --> 00:04:57.610 when we call next again, this one continues, 87 00:04:57.610 --> 00:05:00.320 then it asks to continue this one, 88 00:05:00.320 --> 00:05:02.394 which then gets the next value, returns it here 89 00:05:02.394 --> 00:05:04.447 and then it yields it back to us, 90 00:05:04.447 --> 00:05:07.330 so there's a chain here of calls 91 00:05:07.330 --> 00:05:11.000 that are going down our functions, 92 00:05:11.000 --> 00:05:13.050 one function is running another, which is running another, 93 00:05:13.050 --> 00:05:16.673 but really none of these are running, until we call next, 94 00:05:16.673 --> 00:05:21.673 so we can use next to start this chain of run times, 95 00:05:21.940 --> 00:05:23.170 which is pretty awesome. 96 00:05:25.898 --> 00:05:29.747 Okay, now let's go over to look at something else, 97 00:05:29.747 --> 00:05:32.070 in the next video, we're going to see 98 00:05:32.070 --> 00:05:35.230 how the yield statement can be used to receive data, 99 00:05:35.230 --> 00:05:36.180 I'll see you there.