Wednesday, December 23, 2020

Core Python - Python program to create two decorator functions and use them in calling functions


Core Python - Python program to create two decorators

# decorator increments value of function by 5

def decorator1(fun):

    def inner():

    value = fun()

    return value + 5

    return inner

# decorator that doubles value of a  function

def decorator2(fun):

    def inner():

    value = fun()

    return value*2

    return inner

# take a function to which decorator  could be applied

def num():

    return 10

# call num() function and apply decor1  and then decor

result_fun = decorator1(decorator2(num))

print(result_fun)

print(result_fun)



Output

25



One can apply the decorators to num() function using '@' symbol .

No comments:

Post a Comment