Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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 yield statement enables generators
  • How to use multiple Python yield statements 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?

Download

Sample Code (.zip)

25.2 KB

Download

Course Slides (.pdf)

7.3 MB

Comments & Discussion

davevikram on June 16, 2020

This is my first exposure to Generators. I found easy to follow with the samples provided.

Super on June 17, 2020

This is really good. Thanks :-)

John Berliner on June 17, 2020

Very helpful. The flat file parser is a really interesting and useful example.

drawdoowmij on June 17, 2020

Really nice introduction to generators. Thanks!!

Alan ODannel on June 18, 2020

Very good introduction to generators. Nice examples as well. Thank you.

avalidzy on June 21, 2020

Excellent presentation! Would watch it again. Will find a way to use generators with Django. Thanks!

Sandivya Saxena on June 21, 2020

Very informative!

mikesult on June 25, 2020

Thanks Christian. I learned a lot. Nice examples of using generators and I especially liked the example of chaining them together. Also I liked the exploration in the REPL and the tip of using sys.getsizeof(something) and cProfile.run(‘something()’) for getting memory and performance info.

Become a Member to join the conversation.