
Understanding Python Iterators
Explore the concept of Python iterators, how they are used in for loops with various container objects, and how they work behind the scenes. Learn about the iterator protocol, iterating over iterable objects, and incorporating iterator behavior into user-defined classes.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Python Iterators By Dr. Ziad Al-Sharif
Iterators Iterators By now, you ve probably noticed that most container objects can be used in a for statement: >>>for e in[1, 2, 3]: print(e) >>>for e in(1, 2, 3): print(e) >>>for k in{ one :1, two :2}: print(k) >>>for char in "123": print(char) >>>for line inopen("myfile.txt"): print(line)
Iterators Iterators This style of access is clear, concise, and convenient. The use of iterators pervades and unifies Python. Behind the scenes, the for statement calls iter()on the container object. The function returns an iterator object that defines the method next() which accesses elements in the container one at a time. When there are no more elements, next() raises a StopIteration exception which tells the for loop to terminate.
Iterators Iterators Iterator in python is an object that is used to iterate iterable objects such as: list, tuple, dict, set, etc. The iterator object is initialized using the iter() method. It uses the next() method for iteration. used to iterate over >>> s = 'abc' >>> iter_obj = iter(s) >>> while True: try: print(next(iter_obj)) except StopIteration: break a b c >>>
Iterators: Simple Example Iterators: Simple Example >>> s = abc >>> it = iter(s) >>> it <str_iterator object at 0x0000027BFC78D2E0> >>> next(it) 'a' >>> next(it) 'b' >>> next(it) 'c' >>> next(it) Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> next(it) StopIteration >>>
Iterators and User Defined Classes Iterators and User Defined Classes Having seen the mechanics behind the iterator protocol, it is easy to add iterator behavior to our classes. Define a __iter__() method which returns an object with a next() method. If the class defines next(), then __iter__() can just return self
Iterators: Example Iterators: Example classReverse: "Iterator for looping over a sequence backwards" def__init__(self, data): self.data = data self.index = len(data) def__iter__(self): returnself def__next__(self): ifself.index == 0: raiseStopIteration self.index = self.index - 1 returnself.data[self.index] >>> for char in Reverse('spam'): print(char) m a p s >>>
Python Generators By Dr. Ziad Al-Sharif
Generators Generators Generators are a simple and powerful tool for creating iterators They are written like regular functions but use the yield statement whenever they want to return data. Each time the next() is called, the generator it it remembers all the data values and which statement was last remembers all the data values and which statement was last executed An example shows that generators can be trivially easy to create: generator resumes resumes where it is left-off executed >>>defreverse(data): for i in range(len(data)-1, -1, -1): yield data[i] >>> >>>for c inreverse( abc ): print(c) c b a >>>
Generators Generators Anything that can be done with generators can also be done with class based iterators as described in the previous section. What makes generators so compact is that the __ are created automatically. Another key feature is that the local variables and execution state are automatically saved between calls. This made the function easier to write and much more clear than an approach using class variables like self.index and self.data. __iter iter__() __() and next() next() methods In addition to automatic method creation and saving program state, when generators terminate, they automatically raise StopIteration. In combination, these features make it easy to create iterators with no more effort than writing a regular function.
Generators: Generators: Exercise Exercise # A simple generator for Fibonacci Numbers def fib(n): # Initialize first two Fibonacci Numbers a, b = 0, 1 # One by one yield next Fibonacci Number while a < n: yield a a, b = b, a + b # Create a generator object gen_obj = fib(5) # Iterating over the generator object using next print(gen_obj.__next__()) print(gen_obj.__next__()) print(next(gen_obj)) print(next(gen_obj)) print(next(gen_obj)) print("..............") # Iterating over the generator object using for for i in fib(5): print(i)
References References Functional Programming HOWTO https://docs.python.org/3/howto/functional.html Iterators https://docs.python.org/3/howto/functional.html#iterators Generators https://docs.python.org/3/howto/functional.html#generators PEP 234 -- Iterators https://www.python.org/dev/peps/pep-0234/ Iterator Objects https://docs.python.org/3/c-api/iterator.html