Get Upto 50% Offer | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Generators and Decorators

And the next topic, that I would like to discuss is Generators and Decorators

Python Generator:

The python generators give an easy way of creating iterators. These generators instead of returning the function from the return statement use the "yield” keyword. These are the generator version of the list comprehensions.

If the function contains at least one “yield” statement, it becomes a generator function. Both the yield and return will return some value from the function.

So now you people might be thinking of

 What is the difference between Yield and return?

The return statement terminates the function entirely. And the yield function passes the function. This can be done by saving all the states and later continues from there on the successive calls.

And have you ever think of

What is the difference between a generator and a normal function?

A generator can contain more than one yield statement.

When the iteration object calls the generator, execution does not start immediately.

There is no necessity to call the functions. Additionally, the generator automatically calls methods like iter() and next. Usually next() will iterate these functions

When the function gets yield, the function is paused and the control is transferred to the caller.

Between the successive calls, local variables and their states were remembered.

Finally, if the function terminates, the generator automatically calls the stop iteration()

Click here to know the Advantages and disadvantages of python

Why python uses generators?

Python uses the concept of generators due to several reasons. Let us discuss some of them.

1.Easy to implement:

We can implement the generators easily in a simple and clear manner. It considerably reduces the code when compared to iterators. This is because unlike iterators, generators can call the functions like iter() and next() automatically.

2.Memory efficient:

Prior to the returning of the result, a normal function creates an entire sequence of memory. But if the item number is large memory becomes an overkill. But with generators, the sequence is memory friendly. Because it produces one item at a time.

3.Infinite stream representation:

Generators are an excellent medium to represent infinite steam data. Since the generators produce only one at a time, infinite streams can be stored in the memory.

.Ex:

def gen():

a = 10

print('This is printed first')

# Generator function contains yield statements

yield a

a += 1

print('This is printed second')

yield a

a += 1

print('This is printed at last')

yield a

# Using for loop

for i in gen():

print(i)

And  the next topic that we discuss is about the python decorators:

Decorators:

So far in the previous topics, we have seen about many but –in python functions. These functions have already their own logic. But there are some cases, where we need to change the logic of the built-in functions.

Also check more information on Why can we use python Generators?

Do you think python allows you to change the logic of the built-in functions?

The answer is yes. We can implement the concept of decorators using built-in functions. So

What is  Python Decorator?

A decorator is a python interesting features that add functionality to the existing code. This is also called metaprogramming. Decorators are also known as the metaprogramming.  It was said because it was trying to modify another programming part at compile time. Decorators allow us to wrap up another function in order to extend the behavior of another function.

In decorators, functions are taken as the argument in another function. And inside the wrapper function, we can call the decorators. Using the decorators' concept is easy, but the implementation of these decorators is a bit complicated. We can easily implement it with the support of python online training.

In Python, we can implement  decorators concept in  two ways:

Class decorators

Function decorators

Usually, a decorator is any callable object that is used to modify the function (or) the class. A reference to the function (or) class is passed to the decorator and the decorator returns the modified function (or), class. Usually, the modified functions (or) classes contains the function (or) class call.

So I would like to leave the decorators code as an example. If you struck up anywhere, feel free to contact it online training.