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

Talk Python's Mastodon Michael Kennedy's Mastodon