View This Site In Your Own Language

Decorators in python

decorators in python

In Decorators, functions are taken as the argument into another function and then called inside the wrapper function.

In [13]:
12345678# Create a function to add two numbers

def addition(x, y):
    print(x+y)
    
# calling the addition function
addition(101, 120)
12221

Our function is working nicely. We want to use this function as an argument to our wrapper function. Let's create our wrapper function and pass 'addition' function as a parameter into wrapper function.

In [18]:
123456789101112a, b = 101, 120

def wrapper(function):
    return function(var1, var2)

def addition(x, y):
    print(a+b)

addition = wrapper(addition)

addition(a, b)
1234567891011121314151617---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-4894f656872f> in <module>
      7     print(a+b)
      8 
----> 9 addition = wrapper(addition)
     10 
     11 addition(a, b)

<ipython-input-18-4894f656872f> in wrapper(function)
      2 
      3 def wrapper(function):
----> 4     return function(var1, var2)
      5 
      6 def addition(x, y):

NameError: name 'var1' is not defined

This gives use 'NameError' as name 'var1' is not defined. So, what can we do to solve this issue?
We may create another function inside wrapper function to input arguments.

In [21]:
1234567891011121314a, b = 101, 120

def wrapper(function): # wrapper function will input function as parameter
    def inner(var1, var2): # inner function will input variables as parameters
        function(var1, var2)
    return inner

def addition(x, y):
    print(a+b)
    
addition = wrapper(addition)

addition(a, b)
12221

def addition(x, y): print(a+b) addition = wrapper(addition) addition(a, b)

We may replace the above code with the following code.

@wrapper def addition(x, y): print(a+b) addition(a, b)

In [22]:
12345678910111213a, b = 101, 120

def wrapper(function):
    def inner(var1, var2):
        function(var1, var2)
    return inner

@wrapper
def addition(x, y):
    print(a+b)

addition(a, b)
12221

Problem¶

See the problem by clicking on the following link: Standardize Mobile Number Using Decorators

In [3]:
12345678910111213def wrapper(function):
    def fun(phoneNumber):
        function(["+91 "+c[-10:-5]+" "+c[-5:] for c in phoneNumber])
    return fun

@wrapper
def sort_phone(l):
    print(*sorted(l), sep='\n')

n = 3
number = ['07895462130', '919875641230', '9195969878']
sort_phone(number)
1234+91 78954 62130
+91 91959 69878
+91 98756 41230

Now, given a list of string type numbers : ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'].
First, find palindromic integers among them. Second, Square all of them. Third, print the sum of the squared palindromic integers.

In [31]:
1234567891011121314151617181920212223242526272829303132333435def wrapper1(function):
    def palindromicInteger():
        n = function()
        palindromicInt = [int(i) for i in n if i == i[::-1]]
        print("Palindromic integers are : ", palindromicInt)
        return palindromicInt
    
    return palindromicInteger

def wrapper2(function):
    def square():
        n = function()
        squared = [i**2 for i in n]
        print("Square of each palindromic integer are : ", squared)
        return squared
    
    return square

def wrapper3(function):
    def addition():
        n = function()
        result = sum(n)
        print("Sum of the squared palindromic integers are : ", result)
        return result
    
    return addition

@wrapper3
@wrapper2
@wrapper1
def number():
    return ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15']

print(number())
12345Palindromic integers are :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
Square of each palindromic integer are :  [1, 4, 9, 16, 25, 36, 49, 64, 81, 121]
Sum of the squared palindromic integers are :  406
406

@wrapper3 @wrapper2 @wrapper1 def number()

Above code indicates wrapper3(wrapper2(wrapper1(number)))

In [ ]:
Decorators in python Decorators in python Reviewed by Ikram on 7/15/2021 04:42:00 PM Rating: 5

No comments:

Powered by Blogger.