Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Python Generators 101 (Summary)

This lesson is from the Real Python video course by Christian Mondorf.

00:00 Welcome to our very last video. This is the concluding video. The first thing I’d like to do as we wrap up is to revisit the slide from the very first video. So, on this slide, I told you what you were going to learn in this course and I guess you could say I made a promise, so here we can check if I kept my word. We went over what generators are and how you would use them.

00:20 So, they’re basically structures which allow you to iterate and they return one value at a time, or rather, they yield one value at a time. And that’s quite nice because it saves a lot of memory.

00:33 Other iterator structures load everything into memory at once and so they have, or can have, a very significant memory footprint. There’s a tradeoff here and that is that generators save a lot of memory, but they do this at the expense of speed.

00:46 So whether or not they’re a good solution for you depends, really, on your use case and what it is that you’re trying to achieve.

00:52 I showed you how to create generator functions, the main thing here is to use the keyword yield, and I showed you how to use generator expressions.

01:01 They’re basically like list comprehensions, except they use parentheses instead of square brackets. We looked at the Python yield statement.

01:08 This is what makes a generator function a generator function, as opposed to just a normal function, and you use them by using the yield keyword instead of the return keyword. And yield actually has two uses, we’ll get back to that in a moment. You can use multiple yield statements in a single function. When this generator function is called, it will run until the next yield statement, and that’s where it’ll stop and it’ll wait. And finally, we looked at some advanced generator methods, again, we’ll go back to those in just a second. In the video just before this one, we looked at an example of a data pipeline that uses generators.

01:46 I would encourage you to try that code out by yourself. It’s on the website. You can copy-paste it and then play around with it, that’s really the best way to get the insights from that example to sink in.

01:58 Take it apart, try to understand how it works, try to break it, try to modify it, you know, get creative with it. Okay. So, here is a quick wrap up of the syntax. Generator functions look a lot like functions, except that they use yield instead of return.

02:14 You can have several yield statements. Generator expressions look a lot like list comprehensions, except that you use parentheses instead of square brackets.

02:24 This is easy to remember if you keep in mind that square brackets are used when you define lists. So, lists comprehensions—square brackets, generator expressions—parentheses.

02:36 The three advanced generator methods we saw were send(), throw(), close(). send() is sort of the trickiest one. send() allows you to send a value into the generator, and this is received at the spot where the generator stopped, so where the yield keyword is used, and it can be used to populate a variable within that generator.

02:56 Just keep in mind that this only works if the generator has been initialized first, so you can’t just do this on an unstarted generator. throw() allows you to catch exceptions and select the error type and the error message, which will be returned.

03:11 And finally, close() allows you to close a generator so it doesn’t float around and take up space. Okay, so that was it for this video course. I hope you learned a lot, I hope you enjoyed yourself.

03:22 There’s always a lot to discover, a lot to learn on the website. There are countless tutorials, videos, articles. There’s always something exciting, so I encourage you to stop by.

03:31 Thanks for watching, bye bye!

You must own this product to join the conversation.