Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Python generators

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You saw generators and the yield return keyword
0:03 in C#, was super cool.
0:05 Let's see what Python has to offer.
0:07 We're going to see, actually, some of the implementation here
0:09 might be even better than C#.
0:11 So, just to jump-start things a little bit
0:14 I've added the standard counting finite version.
0:20 Let's print out 10 of these Fibonacci numbers here.
0:25 We can do a cool trick, here we say the end
0:27 is actually the inline, instead of backslash N backslash
0:30 maybe backslash R backslash N, or just back backslash N
0:33 is actually comma space.
0:35 So we'd run this, and you run the correct one
0:38 what do you get? Get a cool thing like that.
0:41 Okay that's neat and those look like
0:43 the Fibonacci numbers to me.
0:45 And of course there's the implementation.
0:47 But this is the kind where you ask for a certain amount
0:49 and we saw that we can use generators in C#
0:52 to make a really cool version of this right
0:54 so let's see what we could do there.
0:56 Go like that, and we had while true
0:59 and there was some stuff in here
1:01 we had this but we didn't have the list
1:03 we just used this yield return keyword to say
1:07 here's an item.
1:09 Maybe we could shorten this, to like this if you wanted.
1:13 A little more condensed say
1:14 here's our starting state 0 and 1.
1:16 I didn't use the word next, because next
1:19 is a function that's built into Python
1:21 and it would conflict with it so just this
1:24 of course that's capital.
1:25 So check this out, here's the implementation in Python
1:28 you can see it below as well. So you say, use your tuple, packing and unpacking
1:32 so current and nxt is equal to nxt
1:35 that's the value of current.
1:36 And then nxt = nxt + current. Super cool.
1:40 Now how do we return this and generate this
1:43 iterator type of thing, that we saw in C#?
1:47 In C# we said yield return item, here we just say
1:51 yield item, that's it.
1:54 That is the whole implementation
1:55 of the infinite Fibonacci sequence.
1:58 And by the way, integers in Python are effectively big ints
2:01 they can be billions or huge, huge, huge numbers
2:05 that don't even have names without overflowing
2:07 unlike say 64-bit integers or longs in C#.
2:11 Now this, is obviously not going to work
2:15 we passed the value here so we need to do something
2:18 like this, if in is greater than 1,000 break.
2:22 I think to be exactly precise, we had that before.
2:25 There we go, let's run it again.
2:26 Oh, just like that, it's exactly the same, except for
2:31 let's do a little side-by-side here.
2:35 See if we can fit it on the screen.
2:38 Not really but we can at least get the for loop on there.
2:41 Get it up so we got the for loop.
2:42 Check this out folks.
2:44 So here's the C# version, we're doing all of this
2:48 and then here's the generator and the implementation
2:51 in the Fibonacci, look over here, here's the top function
2:55 going through them, if it's the same break out.
2:59 Do the print with a comma on the end.
3:01 Here's Fibonacci, there's the Fibonacci implementation
3:04 versus this in C#.
3:08 I mean it's glorious you guys.
3:09 It definitely is a little bit different structure
3:11 and it takes a little bit of getting used to
3:12 but there's a lot of examples like this where you're like
3:15 actually that is really simple and clear
3:18 and this is kind of symbol soup, there's symbols everywhere.
3:22 There's like this generic type up here, there's the static
3:25 and the public, and the curly braces, and the semicolons
3:27 and the integers and whoo yes they have purpose in there
3:31 they're not lacking value
3:33 but they definitely sometimes obscure the essence
3:36 of what you're doing and I feel a lot of times
3:38 having many years of experience in C# and in Python
3:42 really the Python stuff is often quite clear.
3:45 We are missing the type information
3:46 like do you really know those are integers?
3:49 Hang tight, we're getting there.
3:50 We're going to cover these foundations first.
3:52 Here is generators both in C# and in Python
3:56 really only major difference is
3:58 you don't say the return type of function
4:00 so you don't need IEnumerable<int> as that
4:02 cause you can't even really say it yet.
4:05 And instead of yield return, you just say yield.
4:08 Super similar right?