Python Generators vs Iterators
Generators and Iterators are Python constructs that lets you loop through data in chunks instead of loading them into memory. This post discusses the subtle differences between them
In Python, both generators and iterators ensure data is not loaded into memory on the whole but rather processed chunk by chunk. But when to use a generator and iterator.
A generator is a Python function that
yeilda result. Every generator is an iterator. A generator function returns a generator object
for i in range(n):
x = generate(3)
print (next(x)) # raises StopIteration
An iterator is a Python object which returns an iterable via
__iter__method. An iterable has
def __init__(self, n):
self.n = n
self.i = -1
self.i += 1
g = GenerateN(3)
print (next(g)) # raises StopIteration
- Generators are function, and Iterators are object-oriented.
- Use generators when you have a large stream of data and you want to loop over them.
- Use iterators to generate a sequence of data.
- Generators are excellent for large loops since it only works on one value at a time.