Decorator
Decorator
Decorator
3/18/2023 2
Dr. D.P.Sharma
Decorator Fundamentals
Everything is an Object:
3/18/2023 3
Dr. D.P.Sharma
Decorator Fundamentals--Function Aliasing:
For the existing function we can give another name, which is nothing but
function aliasing.
Note:
1) def wish(name):
▪ In this example only one function is
2) print("Good Morning:",name)
available but we can call that function
3)
by using either wish name or greeting
4) greeting=wish
name.
5) print(id(wish))
6) print(id(greeting))
7) Output:
8) greeting('DPSharma')
9) wish('DPSharma') 4429336
4429336
Good Morning: DPSharma
Good Morning: DPSharma
3/18/2023 4
Dr. D.P.Sharma
Decorator Fundamentals--Function Aliasing:
For the existing function we can give another name, which is nothing but
function aliasing.
Note:
1) def wish(name):
2) print("Good Morning:",name) ▪ If we delete one name, still we can
3) access that function by using alias
4) greeting=wish name.
5) greeting('DPSharma')
6) wish('DPSharma')
7)
8) del wish
Output:
9) #wish('DPSharma')---🡪 NameError: name 'wish' is not defined
10) greeting(‘Pavan') Good Morning: DPSharma
Good Morning: DPSharma
Good Morning: Pavan
3/18/2023 5
Dr. D.P.Sharma
Decorator Fundamentals--Nested Functions:
We can declare a function inside another function, such type of functions are called
Nested functions.
1) def outer():
2) print("outer function started") Output:
3) def inner():
4) print("inner function execution") outer function started
5) print("outer function calling inner function") outer function calling inner function
6) inner() inner function execution
7) outer()
8) #inner()--🡪 NameError: name 'inner' is not defined
In the above example inner() function is local to outer() function and hence it is not
possible to call directly from outside of outer() function.
3/18/2023 6
Dr. D.P.Sharma
Decorator Fundamentals--Nested Functions:
Note: A function can return another function.
3/18/2023 7
Dr. D.P.Sharma
Decorator Fundamentals--Nested Functions:
Q) What is the difference between the following lines? (With Ref to Last slide)
f1 = outer
f1 = outer()
▪ But in the second case we calling outer() function, which returns inner function. For that inner
function(), we are providing another name f1
3/18/2023 8
Dr. D.P.Sharma
Decorator Fundamentals--Nested Functions:
Note: We can pass function as argument to another function.
Eg1: Eg2:
3/18/2023 9
Dr. D.P.Sharma
Decorator Fundamentals--Important Conclusions:
3/18/2023 10
Dr. D.P.Sharma
Decorator Fundamentals--Defination
▪ Decorator is a function which can take a function as argument and extend its
functionality and returns modified function with extended functionality
def decor(input_func):
def output_function():
.....................
return output_function
3/18/2023 11
Dr. D.P.Sharma
Decorator Fundamentals—Example1
def decor(func):
def inner():
print('Send person to beauty parlour’)
func()
print('Showing a person with full of decoration')
return inner
@decor
def display():
print('Showing a person as it is')
display()
3/18/2023 12
Dr. D.P.Sharma
Decorator Fundamentals—Example2:
def smart_division(func):
def inner(a,b):
if b == 0:
print('Hello Stupid, How we can divide with ZERO|||')
else:
func(a,b)
return inner
@smart_division
def division(a,b):
print(a/b)
division(16,2)
3/18/2023 13
Dr. D.P.Sharma
Decorator--- Important Conclusion:
2. While defining decorator, the number of arguments must be matched. i.e. Arguments of
the original function and the inner function of decorator must be matched.
3/18/2023 14
Dr. D.P.Sharma
Decorator Fundamentals
How to call same function with decorator or without decorator---Method1
def decor(func):
def inner():
print('Send person to beauty parlor’)
func()
print('Showing a person with full of decoration')
return inner
OUTPUT:
def display():
Send person to beauty parlor
print('Showing a person as it is')
Showing a person as it is
Showing a person with full of decoration
Décor_wish=décor(display)
Décor_wish() ### will call the inner function of Decorator
3/18/2023 15
Dr. D.P.Sharma
Decorator Fundamentals
How to call same function with decorator or without decorator---Method2
def decor(func):
def inner():
print('Send person to beauty parlour’)
func()
print('Showing a person with full of decoration')
return inner
def display():
print('Showing a person as it is')
display()
3/18/2023 16
Dr. D.P.Sharma