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.

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.


100% Secure Payments. All major credit & debit cards accepted.