top of page

What is Generator in Python? Why should you use Generators?

Updated: Feb 10, 2023

Generators have been an important part of python ever since they were introduced with PEP 255.


What is a generator?

Generator functions allow you to declare a function that behaves like an iterator.

They allow programmers to make an iterator in a fast, easy and clean way.


What is an iterator?

An iterator is an object that can be iterated(looped) upon. It is used to abstract a container of data to make it behave like an iterable object.


for example, we are using so many iterable objects like string, list, dictionaries...


How to define an iterator?

An iterator is defined by a class that implements the Iterator Protocol. This Protocol looks for two methods within the class: __iter__ and __next__.


What's the advantage of an iterator?

Saving memory space

Iterators don't compute the value of each item when instantiated. They only compute it when you ask for it. This is known as lazy evaluation.


Lazy evaluation is useful when you have a very large data set to compute. It allows you to start using the data immediately, while the whole data set is being computed.


Let's say we want to get all the prime numbers up to 100.

By using an iterator, we are not creating a list of prime numbers in our memory. Instead, we are generating the next prime number every time we request for it.

Here, Primes is instantiated with a maximum value 100. If the next prime is greater or

equal than the max, the iterator will raise a StopIteration exception, which ends the

iterator.

When we request the next element in the iterator, it will increment number by 1 and

check if it's a prime number. If it's not, it will call __next__ again until number is prime. if its prime then returns the number.


Every iteration of the Primes object calls __next__ to generate the next prime number.


Iterators can only be iterated over once. If you try to iterate over primes again, no value will be returned. It will behave like an empty list.


So, iterator Rembers the state and move to next element until it raises StopIteration

exception.


Generators

generator functions allows us to create iterators in a more simple fashion.


Generators introduce the yield statement to python. It works a bit like return, because it returns a value.


The difference is that it saves the state of the function. The next time the function is called, execution continues from where it left off, with the same variable values it had before yielding.


If we transform our Primes iterator into a generator, it will look like this:

We can use Generator Expressions, introduced with PEP 289.


This is the list comprehension equivalent of generators. It works exactly in the sameway as a list comprehension, but the expression is surrounded with () as opposed to [].

This is the beauty of generators in python.


Summary:

  • Generators allow you to create iterators in a very pythonic manner.

  • Iterators allow lazy evaluation, only generating the next element of an iterable object when requested. This is useful for very large data sets.

  • iterators or generators can only be iterated over once.

  • Generator Functions are better than Iterator.

  • Generator Expressions are better than Iterators.


 

Thanks for reading!!!


Your Rating and Review will be appreciated!!


Twitter: @LearnerLandmark


154 views0 comments

Recent Posts

See All
bottom of page