Introduction of Decorator and HOC in Python
In this tutorial we will learn all details about the decorator in python and their uses in real time projects with different scenario.
Simply decorator in python is a function that accepts a function as a parameters and eturn a function. A decorators takes the result of a function , modifies result and return it. we use @function_name to specify a decorator to be applied on another function.
In decorators, functions are taken as a arguments into another function and then called inside the wrapper function.
lets take a scenario
def message():
print("hello nepal")
we have a function to display message "hello nepal" , then we need to add the current datetime inside the message function without modifying it. in this case decorator comes in hand.
def message():
print("hello nepal")
def decor(fun):
#it takes parameters as function
def inner():
fun() #calling the function
from datetime import datetime
print("Current datetime is",datetime.now())
return inner
inner_fun=decor(message) # all the function of inner() is here
print(inner_fun())
"""
hello nepal
Current datetime is 2023-05-21 13:48:38.790379
None
"""
# you call used @sign also too
def decor(fun):
#it takes parameters as function
def inner():
fun() #calling the function
from datetime import datetime
print("Current datetime is",datetime.now())
return inner
@decor
def message():
print("hello nepal")
#you call directly called it
message()
"""
hello nepal
Current datetime is 2023-05-21 13:52:04.786104
"""
Higher Order Function (HOC)
if function pass to the function the it is called higher order function.
1.filter()
filter function is used to filter out the element of an iterable depending on a function that tests each element in the sequence to be true or false. it returns those elements of sequence , for which function is true.
#syntax
"""
filter(function_name,iterable)
where: function_name= it is the function which takes each element in sequence
iterable=either may be list,tuple,container e.t.c
"""
a=list(range(1,11))
def odd(n):
if n%2!=0:
return True
my_list=list(filter(odd,a))
print(my_list)
result=list(filter(lambda x:(x%2!=0),a))
print(result)
2.map() function
These function execute a specified function on each element of the iteration and performs changes on element.
#syntax
"""
map(function_name,iterable)
where: function_name= it is the function which takes each element in sequence
iterable=either may be list,tuple,container e.t.c
"""
a=list(range(1,11))
def odd(n):
if n%2!=0:
return n+1
else:
return n
my_list=list(map(odd,a))
print(my_list)
result=list(filter(lambda x:(x%2!=0),a))
print(result)
"""
[2, 2, 4, 4, 6, 6, 8, 8, 10, 10]
[1, 3, 5, 7, 9]
"""
3.reduce function
This function is used to reduce a sequence of elements to a single value by producing the element according to the function supplied. it return single value.
this function is the part of functools module so you need to import it.
#syntax
"""
reduce(function_name,iterable)
where: function_name= it is the function which takes each element in sequence
iterable=either may be list,tuple,container e.t.c
"""
from functools import reduce
num=[1,2,3,4,5,6,7,8,9,10]
def add(a,b):
# print(a,b)
return a+b
result=reduce(add,num)
# result=reduce(lambda a,b:a+b,num)
print(result)
#55
Generator
Generators are the function that return a sequence of values. we used yield statement to return the value from function.
yield statement returns the elements from a generator function into a generator object. eg:yield a
next() statement ,this gunction is used to retrive element ny element from generator object eg: next(generator_object)
#generator
def show(a,b):
yield a
yield b
x,y=show(10,20)
print(x)
print(y)
result=show(5,10) # result is generator object here
print(next(result))
print(next(result))
"""
10
20
5
10
"""