Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using generators

From: Python 3 Essential Training

Video: Using generators

A generator object is an object that can be used in the context of an iterable, like for instance in a for loop. Let's take a look at how this is done in Python. We'll make a working copy of generator .py. I'll call it generator-working.py. We'll open that up and we see that here we have a range object being used in the context of an iterator. So o is getting assigned to this range object that's a range of 25 and it's being used in the context of this for loop which is an iterable context and if I run this, you see that we get series of numbers from 0 through 24 and this is how range works.

Using generators

A generator object is an object that can be used in the context of an iterable, like for instance in a for loop. Let's take a look at how this is done in Python. We'll make a working copy of generator .py. I'll call it generator-working.py. We'll open that up and we see that here we have a range object being used in the context of an iterator. So o is getting assigned to this range object that's a range of 25 and it's being used in the context of this for loop which is an iterable context and if I run this, you see that we get series of numbers from 0 through 24 and this is how range works.

Range is non-inclusive, which means that we ask for a range of 0 through 25. What we get 0 through 24, because it's up to but not including the range we specify. Range takes three possible arguments. It only requires 1 and so those three possible arguments are the start and the stop and the step. So we start at 0 and stop at 25 and step by 1, this is the result that we get.

And so start and step default to 0 and 1 and all you have to specify is the stop. If I were to start it say at 5 and step by say 2, save that and run it, you will get 5 through 23. Again, we don't get 25 because it's not inclusive, and you see they are stepping by 2. So what I would like to do here is create our own range object as an exercise to learn how to create a generator object in Python.

It will work exactly like this one, except that it will be inclusive. We'll always get that last number there. So go ahead and reset this to 25 and we will start creating our class. So we'll call this class inclusive_ range and it will have a constructor. And it will have an iterator. These are both special method names in Python. Init with the two underscores before and after it are for constructors and iter like this, with two underscores before it and two underscores after it, makes the object an iterable object.

So this is where we'll put our generator function in here. The constructor will need to be able to do this weird thing with the argument. The first argument and the third argument are optional but the second argument is required. That's going to take a little bit of manipulation on our end and we'll do that in the constructor. We need this arbitrary list of positional arguments. So we'll use the list argument syntax here and then asterisk and args and we'll get the number of arguments by using the length built in.

len(args) like that and if we have less than one argument we are going to raise an error. We'll use TypeError requires at least one argument. Let's say if numargs is less than 1 and we'll use elif numargs == 1, elif numargs == 2.

I like to outline my code like this sometimes and then go back and fill it in. Numargs = 3 or else we'll raise a TypeError. Expected at most three arguments and got this other number. Now we can go ahead and fill in each of these conditions. If we get just one argument then we know that's the stop. So we can say self.stop = args sub 0. Otherwise we have got self.start = 0 and self.step = 1, because those are those default values.

If we have two arguments and we know that that's the start and the stop. self.start, self.stop, and we assign that to args and we use our default for step. Finally, if we have three arguments, then we know that's all of them, start, stop and step. So now we have our constructor and our iterator is very easy.

We start by setting the starting point and we have a simple while loop, while i is less than or equal to a stop, then we yield the result-- and we'll get back to that one-- and we increment the iterator by this step. So this is what makes it a generator is the yield statement.

Yield works just like return but with the significant difference. If I were to use return here, it would return the value and the next time the iterator was called it would start at the beginning of the function. By using yield instead it returns the value and the next time the function is called execution picks up right after the yield statement. So the way this will run is it will set the starting point and it will test the while loop and assuming that the starting point is less than or equal to the stop point, it will yield the value. Do that's the starting point and then the next time the iterator is called, it will increment that value and test the while loop again, and then it will yield the next value and then it will increment and we'll yield the next value and this allows it to operate as an iterator.

So by having the yield statement inside the function, that makes the function a generator and what a generator generates is an iterable object. We need to change this to our inclusive_range. I have a little typo here. self.step. Save that and run it and there we have a range all the way up to and including our stop, and so let's go ahead and test out our constructor.

I'll change the start point to 5 and now it start to 5 and go up to 25. Save that and run it, and there it, starts at 5 and goes all the way up to 5. Let's have it step by 2 instead of stepping by 1. Save that and run it. Now we see it steps by 2. We can change that to 7 and it will still do what we expect. That's great and now let's call it with no arguments and we'll test our error conditions.

No arguments, we would expect it to get that TypeError and there it is, TypeError requires at least one argument and if we give it four arguments, let's say 1, 25, and 3 and 14, we expect to get this other type error here and there it is, expected at most three arguments and got four. So we have successfully duplicated the range generator function, save that and run it, except that ours is inclusive.

That was really easy to do. So this is how you make a generator object is by using the iter method in your class and now your object becomes iterable. It becomes a generator and when the object is used in the context of an iterable, like for instance, in this for loop, then that iter methods gets called automatically and you do not have to do something like .iter.

You can simply use the object in that context. In fact I don't even have to create an intermediate variable. So I can take this line out all together and put it in this place here and you will see that range is used like this very often. So I can save that and run it and there we have a drop in replacement for range except that this one is inclusive and that's how you make the generator object in Python. It's a very convenient and very powerful technique.

You will find yourself using it in some places that you won't anticipate. I have used it in database applications where I have a very specific database application with a specific class that simply steps through a particular type of a query. I have used it in parsing files, where I want to get a certain pattern out of a file. There is all kinds of applications for this and you will find that you actually use it more often than you might think.

Show transcript

This video is part of

Image for Python 3 Essential Training
Python 3 Essential Training

87 video lessons · 39578 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 5m 14s
    1. Welcome
      1m 32s
    2. Understanding prerequisites for Python
      2m 4s
    3. Using the exercise files
      1m 38s
  2. 33m 29s
    1. Getting started with "Hello World"
      4m 43s
    2. Selecting code with conditionals
      4m 45s
    3. Repeating code with a loop
      4m 13s
    4. Reusing code with a function
      2m 43s
    5. Creating sequences with generator functions
      2m 46s
    6. Reusing code and data with a class
      4m 39s
    7. Greater reusability with inheritance and polymorphism
      7m 17s
    8. Handling errors with exceptions
      2m 23s
  3. 22m 32s
    1. Installing Python 3 and Eclipse for Windows
      11m 24s
    2. Installing Python 3 and Eclipse for Mac
      11m 8s
  4. 28m 0s
    1. Creating a main script
      3m 27s
    2. Understanding whitespace in Python
      4m 8s
    3. Commenting code
      3m 28s
    4. Assigning values
      3m 37s
    5. Selecting code and values with conditionals
      4m 46s
    6. Creating and using functions
      3m 54s
    7. Creating and using objects
      4m 40s
  5. 31m 23s
    1. Understanding variables and objects in Python
      2m 46s
    2. Distinguishing mutable and immutable objects
      2m 41s
    3. Using numbers
      3m 34s
    4. Using strings
      6m 38s
    5. Aggregating values with lists and tuples
      4m 55s
    6. Creating associative lists with dictionaries
      4m 24s
    7. Finding the type and identity of a variable
      4m 45s
    8. Specifying logical values with True and False
      1m 40s
  6. 9m 42s
    1. Selecting code with if and else conditional statements
      2m 22s
    2. Setting multiple choices with elif
      2m 14s
    3. Understanding other strategies for multiple choices
      2m 38s
    4. Using the conditional expression
      2m 28s
  7. 11m 26s
    1. Creating loops with while
      1m 27s
    2. Iterating with for
      3m 54s
    3. Enumerating iterators
      3m 22s
    4. Controlling loop flow with break, continue, and else
      2m 43s
  8. 23m 28s
    1. Performing simple arithmetic
      2m 14s
    2. Operating on bitwise values
      3m 30s
    3. Comparing values
      3m 32s
    4. Operating on Boolean values
      2m 59s
    5. Operating on parts of a container with the slice operator
      6m 52s
    6. Understanding operator precedence
      4m 21s
  9. 11m 34s
    1. Using the re module
      1m 4s
    2. Searching with regular expressions
      3m 12s
    3. Replacing with regular expressions
      3m 29s
    4. Reusing regular expressions with re.compile
      3m 49s
  10. 9m 10s
    1. Learning how exceptions work
      1m 18s
    2. Handling exceptions
      4m 15s
    3. Raising exceptions
      3m 37s
  11. 23m 1s
    1. Defining functions
      6m 23s
    2. Using lists of arguments
      2m 26s
    3. Using named function arguments
      4m 32s
    4. Returning values from functions
      1m 55s
    5. Creating a sequence with a generator function
      7m 45s
  12. 47m 29s
    1. Understanding classes and objects
      5m 12s
    2. Using methods
      6m 12s
    3. Using object data
      10m 4s
    4. Understanding inheritance
      5m 11s
    5. Applying polymorphism to classes
      7m 13s
    6. Using generators
      9m 48s
    7. Using decorators
      3m 49s
  13. 18m 54s
    1. Understanding strings as objects
      3m 25s
    2. Working with common string methods
      5m 24s
    3. Formatting strings with str.format
      5m 31s
    4. Splitting and joining strings
      2m 49s
    5. Finding and using standard string methods
      1m 45s
  14. 25m 27s
    1. Creating sequences with tuples and lists
      4m 6s
    2. Operating on sequences with built-in methods
      5m 50s
    3. Organizing data with dictionaries
      4m 56s
    4. Operating on character data with bytes and byte arrays
      10m 35s
  15. 11m 46s
    1. Opening files
      2m 4s
    2. Reading and writing text files
      4m 33s
    3. Reading and writing binary files
      5m 9s
  16. 21m 27s
    1. Creating a database with SQLite 3
      6m 56s
    2. Creating, retrieving, updating, and deleting records
      7m 31s
    3. Creating a database object
      7m 0s
  17. 18m 27s
    1. Using standard library modules
      8m 0s
    2. Finding third-party modules
      5m 47s
    3. Creating a module
      4m 40s
  18. 23m 11s
    1. Dealing with syntax errors
      8m 19s
    2. Dealing with runtime errors
      4m 0s
    3. Dealing with logical errors
      4m 22s
    4. Using unit tests
      6m 30s
  19. 19m 56s
    1. Normalizing a database interface
      6m 39s
    2. Deconstructing a database application
      8m 9s
    3. Displaying random entries from a database
      5m 8s
  20. 29s
    1. Goodbye
      29s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Python 3 Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.