Generators are just like functions which give us a sequence of values one as an iterable (which can be iterated upon using loops). Generators contain yield statements just as functions contain return statements.
Example: Generators
defm():yield'Mahesh'yield'Suresh'g =m()print(g)print(type(g))for y in g:print(y)
Output:
<generator object m at 0x7fc5cabc3150><class'generator'>
Mahesh
Suresh
Example: Generators
defm(x, y):while x<=y:yield xx+=1g =m(5,10)for y in g:print(y)
Output:
5678910
next function in Python:
If we want to retrieve elements from a generator, we can use the next function on the iterator returned by the generator. This is the other way of getting the elements from the generator. (The first way is looping in through it as in the examples above).
A decorator is a special function which adds some extra functionality to an existing function. For now let’s understand a decorator as:
1. A decorator is a function that accepts a function as a parameter and returns a function.
2. Decorators are useful to perform some additional processing required by a function.
Steps to create decorator:
Step1: Decorator takes a function as an argument
def decor(func): #Here 'func' is the argument /parameter which receives the function
Step2: Decorator body should have an inner function
def decor(func): #Here 'func' is the argument /parameter which receives the function
def inner_function():
body of inner function
Step3: Decorator should return a function
def decor(func):#Here'func' is the argument /parameter which receives the function
def inner_function():
body of inner functionreturn inner_function #Decor returns the inner_function
Step4: The extra functionality which you want to add to a function can be added in the body of the inner_function.
Let’s create a function which takes two arguments and prints the sum of them.
Example: Add Function
defadd(a,b):res = a + breturn resprint(add(20,30))print(add(-10,5))
Output:
50-5
Now, I wish to add some extra functionality of adding the two numbers only if they are positive. If any number is negative, then I wish to take it as 0 during adding. For adding this extra functionality let create a decorator.
defdecor(func):#Here ‘func’ is the the argument/parameter which receives the functiondefinner_function(x,y):if x<0:x =0if y<0:y =0returnfunc(x,y)return inner_function#Decor returns the func passed to it.
We have created our decorator and now let’s use it with our add function from add function
add = decor(add)
With the above statement, we are passing the add function as parameter to the decorator function, which is returning inner_function. Now, the inner_function object or address will be overridden in the ‘add’ because we are capturing the returned function in it.
After this, whenever we call add, the execution goes to inner_function in the decorator.
add(-10,20)
In inner_function, we are doing the extra logic for checking whether the arguments are positive or not. If not positive we are assigning them with zero. And we are passing the processed values to the original add function which was sent to the decorator.
Generators :-
Generators are just like functions which give us a sequence of values one as an iterable (which can be iterated upon using loops). Generators contain yield statements just as functions contain return statements.
Example: Generators
Output:
Example: Generators
Output:
next function in Python:
If we want to retrieve elements from a generator, we can use the next function on the iterator returned by the generator. This is the other way of getting the elements from the generator. (The first way is looping in through it as in the examples above).
Example: Generators with next function
Output:
Decorators :-
A decorator is a special function which adds some extra functionality to an existing function. For now let’s understand a decorator as:
1. A decorator is a function that accepts a function as a parameter and returns a function.
2. Decorators are useful to perform some additional processing required by a function.
Steps to create decorator:
Step1: Decorator takes a function as an argument
Step2: Decorator body should have an inner function
Step3: Decorator should return a function
Step4: The extra functionality which you want to add to a function can be added in the body of the inner_function.
Let’s create a function which takes two arguments and prints the sum of them.
Example: Add Function
Output:
Now, I wish to add some extra functionality of adding the two numbers only if they are positive. If any number is negative, then I wish to take it as 0 during adding. For adding this extra functionality let create a decorator.
We have created our decorator and now let’s use it with our add function from add function
add = decor(add)
With the above statement, we are passing the add function as parameter to the decorator function, which is returning inner_function. Now, the inner_function object or address will be overridden in the ‘add’ because we are capturing the returned function in it.
After this, whenever we call add, the execution goes to inner_function in the decorator.
add(-10,20)
In inner_function, we are doing the extra logic for checking whether the arguments are positive or not. If not positive we are assigning them with zero. And we are passing the processed values to the original add function which was sent to the decorator.
Our final code will be
Example: Decorator Function
Output :
Thank you for contacting us. Pleasure to answers this question.