Generators in Python with Example

Generators in Python with Example

Python generators are used to create the iterators, but with a different approach. Generators are simple functions that return an iterable set of items, one at a time, in a unique way. There is a lot of things in building iterators in Python. We have to implement a class with __iter__() and __next__() method.

Python Generators

What are generators in Python?

There is a lot of overhead in building an iterator in Python. we have to implement a class with __iter__() and __next__() method, keep track of internal states, raise StopIteration when there was no values to be returned etc.

How to create a generator in Python?

It is very easy to create a generator in Python programming. It is as easy as defining a normal function with yield statement instead of a return statement.

The main difference between normal function and Python Generators is that the return statement terminates the function entirely; yield statement pauses the function saving all its states and variable values and later continues from there on successive calls.

See the following example of Generators in Python.

def createGenerator():
    i = 1
    print('Python')
    print(i)
    yield i

    i += 1
    print('PHP')
    print(i)
    yield i

    i += 1
    print('java')
    print(i)
    yield i

gen = createGenerator()
next(gen)
next(gen)
next(gen)

See the following output of above python program:

 Python
 1
 PHP
 2
 java
 3

One interesting thing to note in the above example is that the value of the variable i is remembered between each call.

Unlike any normal functions, the local variables are not destroyed when the function yields. In addition to that, the generator object can be iterated only once.

For Loops with Generators

We can use the python for loop with Generators. And display the values with executing the next() function.

You can see the following example:

def createGenerator():
    i = 1
    print('Ronin')
    yield i

    i += 1
    print('RedSkull')
    yield i

    i += 1
    print('Rocket')
    yield i

for g in createGenerator():
    print(g)

See the following output of the above python program:

 python
 1
 PHP
 2
 Java
 3

Python Generator

Simple generators can be easily created on the fly using generator expressions. It makes constructing generators easy. 

The same thing in lambda function creates an anonymous function, generator expression creates the anonymous generator function.

The syntax for generator expression is similar to that of the list comprehension in Python. But the square brackets are replaced with round parentheses.

The significant difference between the list comprehension and the generator expression is that while list comprehension produces the entire list, generator expression produces one item at a time.

They are kind of lazy, producing items only when asked for. For this reason, a generator expression is much more memory efficient than list comprehension.

See the below example.

listK = [18, 19, 21, 29, 46]
result = (i ** 2 for i in listK)
print(next(result))
print(next(result))
print(next(result))
print(next(result))

See the following output of the above python program:

 324
 361
 441
 841

Generator expression can be used inside the functions. When used in such a way, the round parentheses can be used. 

We can see above that the generator expression did not produce the required result immediately. Instead, it returned a generator object which produces elements on demand.

Python Yield

The yield statement suspends the function’s execution and sends the value back to the caller, but retains enough state to enable function to resume where it is left off.

When resumed, a function continues execution immediately after the last yield run.

This allows its code to produce the series of values over time, rather than computing them at once and sending them back like a list.

Yield is used in Python generators.

The generator function is defined as a regular function, but whenever it needs to generate a value, it does so with a yield keyword rather than return.

If the body of the def contains yield, the function automatically becomes a generator function.

Differences between Generator and a Normal function

Here we will discuss differences between Generator and a Normal function:

  1. Generator function contains one or more yield statements.
  2. When the generator function called, it returns an object (iterator) but does not start execution immediately.
  3. Functions like __iter__() and __next__() are implemented automatically. So we can iterate through the elements using the next() function.
  4. Once the function yield statement is executed, the function is paused and transfers the control to the caller.
  5. Local variables and their states are remembered between successive calls.
  6. Finally, when the function terminates, StopIteration is raised automatically on further calls.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *