An iterator is a special class that provides a sequence of items, generally used in a for loop. An iterator object is functionally similar to a generator function.
- [Instructor] An iterator is a class that provides a sequence of items, generally used in a loop. Here in Komodo, I've opened a working copy of iterator.py from chapter nine of the exercise files. This is an iterator class implementation of the inclusive range application, it's functionally identical to the generator from the functions chapter. So, let's take a look at how this works. Here we have our constructor and our constructor simply sets up all of the variables and it checks the arguments, it checks how many arguments we have, if there's just one, that's the stop, if there's two, it's start and stop, and if there's three arguments, it's start, stop, and step.
Just like the range function, the built-in range function. And if we have the wrong number of arguments, we get a TypeError exception, again, we'll cover exceptions in detail later on in this course. And we initialize the starting point of our iterator, and then we have this special iterator method I-T-E-R with the double underscores on either side of it, and this simply identifies this object as an iterator object, and then there's the __next__ function, which is the iteration itself, a construct like the for loop is going to look for this __next__ function in order to treat this as an iterator, and in order to use it for iteration.
If we've reached stop, we raise the StopIteration exception, otherwise, we go ahead and increment and return the value, and you'll notice there's no yield here, yield was implemented later than iterators, and generator functions are actually a simpler way to do exactly the same thing, but iterators are still very common, and so here we have our application, for n in inclusive range, and we use this just like we would with range, and so when I run this, you see we get our range and come down here to the end again, and see where we've passed it 25, and so we get zero through 25 including the 25, and if we use this with just the built-in range function, you notice that we get that without the, it's not inclusive, it doesn't include the 25.
I can set this up to start, simply cells, so I can say start at five, and when I run this, it's five through 25, and I can tell it to step by five, and when I run this, you notice it steps by five instead of by one, or I can do some other number, here I can have it step by three, or whatever. So, an iterator is functionally identical to a generator function, a generator function is often easier to implement and will work just as well, but it's good to understand how iterators work, and you'll certainly see them in the wild.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
Learning Python Web Penetration Testingwith Christian Martorella2h 49m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
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.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.