Python Generators 101 (Summary)
In this course, you’ve learned about generator functions and generator expressions.
You now know:
- How to use and write generator functions and generator expressions
- How the all-important Python
yieldstatement enables generators
- How to use multiple Python
yieldstatements in a generator function
- How to use
.send()to send data to a generator
- How to use
.throw()to raise generator exceptions
- How to use
.close()to stop a generator’s iteration
- How to build a generator pipeline to efficiently process large CSV files
How have generators helped you in your work or projects? If you’re just learning about them, then how do you plan to use them in the future? Did you find a good solution to the data pipeline problem?
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
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.
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.
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
The three advanced generator methods we saw were
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.
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.
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.
Become a Member to join the conversation.