0% found this document useful (0 votes)
15 views16 pages

Decorator

This has notes about Decorator in python programming

Uploaded by

Swaty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views16 pages

Decorator

This has notes about Decorator in python programming

Uploaded by

Swaty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Python Programming

Decorator

3/18/2023 2
Dr. D.P.Sharma
Decorator Fundamentals
Everything is an Object:

▪ In Python every thing is treated as object.

▪ Even functions also internally treated as an objects only.

1) def f1(): Output:


2) print("Hello")
3) print(f1) <function f1 at 0x00419618>
4) print(id(f1)) 4298264

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.

1) def outer(): Output:


2) print("outer function started")
3) def inner(): outer function started
4) print("inner function execution") outer function returning inner function
5) print("outer function returning inner function") inner function execution
6) return inner inner function execution
7) f1=outer() inner function execution
8) f1()
9) f1()
10) f1()

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()

▪ In the first case, we are providing another name f1 (function aliasing).

▪ 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:

filter(function,sequence) def f1(func):


map(function,sequence) func()
def f2():
reduce(function,sequence)
print(“f2 function”)
f1(f2)

3/18/2023 9
Dr. D.P.Sharma
Decorator Fundamentals--Important Conclusions:

▪ We can assign another name to existing function===>Function Aliasing.

▪ We can define one function inside another function===> Nested functions.

▪ A Function can return another function.

▪ We can pass a function as argument to anotherfunction.

eg: filter(), map(), reduce()

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

▪ The main objective of decorator functions is to extend the functionality of existing


function without modifying that 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:

1. Decorator function should be defined first and then use.

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

##@decor convert this line as comment or remove it

def display():
print('Showing a person as it is')
display()

3/18/2023 16
Dr. D.P.Sharma

You might also like