Monday, 30 December 2019

Decorator function


def security(name):
    print("Thanks,",name,"Welcome")
#This function can allow all the persons including non-emp
security("Ravi")
"""output:
Thanks, Ravi Welcome
Thanks, Sita Welcome
"""
The above function displays the same msg for all the parameters i.e names. 
 But I want to modify this function to provide different msg for a different name without modifying the original function security() if outsiders enter. If the name is Sita, we can allow, other than Sita , we should not allow.
 
 By using Decorator function:
def decor(func):
    def inner(name):
        if name!='Sita':
            print("Sorry",name,"Please take the pass")
        else:
            func(name)
    return inner
def security(name):
    print("Thanks,",name,"Welcome")
"""output:
>>> modified=decor(security)
>>> modified("ravi")
Sorry ravi Please take the pass
>>> modified('Sita')
Thanks, Sita Welcome
>>> """
           
@Apply décor to the function
def decor(func):
    def inner(name):
        if name!='Sita':
            print("Sorry",name,"Please take the pass")
        else:
            func(name)
    return inner
@decor Apply décor to the below function
def security(name):
    print("Thanks,",name,"Welcome")
"""output:
>>> security("ravi")
Sorry ravi Please take the pass
>>> security("sita")
Sorry sita Please take the pass
>>> security("Sita")
Thanks, Sita Welcome
>>> """
In the above function when we call security() automatically décor() will be executed.
           
Note: décor is not a keyword, it is user-defined word, so instead of décor we can use any name, but while using user-defined word, same word should be used @userdefined

Example:
def modify(func):
    def inner(name):
        if name!='Sita':
            print("Sorry",name,"Please take the pass")
        else:
            func(name)
    return inner
@modify
def security(name):
    print("Thanks,",name,"Welcome")               


Decorator is a function which can take a function as argument and extend its functionality of existing function and returns modified function with extended functionality without modifies the original function.

Steps to use décor function:
1.     Define décor function with existing function name as a formal parameter      Example:                                                                                                                   def décor(fun):       
2.     Define a extend function inside the decorator function, this function decorates the extended functionality of existing function, passed to the decorator function.
Ex: def décor:
             Def inner():
                 ..logic to be extended
3.     Return the inner function or decorated functionality

def div(a,b):
    c=a/b
    return c
"""output:
>>> div(4,0) #leads to run time error
2.0
Here 4 is numerator, 0 is denominator
>>> div(2,4)
0.5
here 2 is numberator, 4 is denominator
>>>

 By using Decorator function:
def div(a,b):
    print(a/b)
#decorator function
def makeup(fun):#this is a decorator function
    def inner(a,b):#this is the inner function that modifies
        if b==0:
            print("Can not be divided")
        else:
            return fun(a,b)
    return inner
result=makeup(div)
result(4,0)
"""output:
Can not be divided"""
Here 4 is numerator, 0 is denominator
By using Decorator function


Related Video link: https://youtu.be/d-qTxNTiZPM
Next: decorator chaining
Prev Using Lambda with filter () function

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...