Iterators_and_Generators.md
November 28, 2023 ยท View on GitHub
Advanced Python Concepts: Iterators and Generators
Iterators
An iterator in Python is an object that can be iterated upon, meaning that you can traverse through all its values. Typically, an iterator is implemented with methods __iter__() and __next__().
Key Concepts
__iter__Method: Returns the iterator object itself and is used inforandinstatements.__next__Method: Returns the next value from the iterator. When there are no more items, it raises aStopIterationexception.
Example
class Count:
def __init__(self, low, high):
self.current = low
self.high = high
def __iter__(self):
return self
def __next__(self):
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
# Using the iterator
for number in Count(1, 3):
print(number) # Outputs: 1 2 3
Generators
Generators are a simpler way to create iterators using functions and the yield statement. A generator function is defined like a normal function but whenever it needs to generate a value, it does so with the yield keyword rather than return.
Key Concepts
yieldStatement: When the generator function is called, it returns an iterator known as a generator. The function execution stops at theyieldstatement and resumes when the next value is requested.- State Preservation: Unlike regular functions, the local variables and their states are remembered between successive calls.
- Lazy Evaluation: Generators produce items one at a time and only when required, leading to increased efficiency, especially when working with large datasets.
Example
def countdown(n):
while n > 0:
yield n
n -= 1
# Using the generator
for number in countdown(3):
print(number) # Outputs: 3 2 1
Conclusion
Iterators and generators are powerful concepts in Python, providing an efficient way to iterate over data. While iterators require a class with __iter__() and __next__(), generators achieve the same with less code. Generators are especially useful for working with large data sets, as they provide data one item at a time and only as needed, thus conserving memory.