Nearby lessons

96 of 108

Python - Decorators

Detailed Tutorial Notes

Function Decorators:

Decorator is a function which can take a function as argument and extend its functionality

and returns modified function with extended functionality.

The main objective of decorator functions is we can extend the functionality of existing

functions without modifies that function.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
def decor(func):
def inner(name):
if name=="Sunny":
print("Hello Sunny Bad Morning")
else:
func(name)
return inner

This function can always print same output for any name

Hello Durga Good Morning

Hello Ravi Good Morning

Hello Sunny Good Morning

But we want to modify this function to provide different message if name is Sunny.

We can do this without touching wish() function by using decorator.

Eg:

8)

9) @decor
10) def wish(name):
11) print("Hello",name,"Good Morning")

12)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello Durga Good Morning
Hello Ravi Good Morning
Hello Sunny Bad Morning

Decorator

Input Function

wish()

new(add some functionality)

inner()

Decorator

Function

Input Function Output Function with

extended Functionality

In the above program whenever we call wish() function automatically decor function will

be executed.

How to call same function with decorator and without decorator:

We should not use @decor

1) def decor(func):
2) def inner(name):
3) if name=="Sunny":
4) print("Hello Sunny Bad Morning")
5) else:
6) func(name)
7) return inner

8)

9)

10) def wish(name):
11) print("Hello",name,"Good Morning")

12)

13) decorfunction=decor(wish)

14)

15) wish("Durga") #decorator wont be executed
16) wish("Sunny") #decorator wont be executed

17)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello Durga Good Morning
Hello Sunny Good Morning
Hello Durga Good Morning
Hello Sunny Bad Morning

Eg 2:

1) def smart_division(func):
2) def inner(a,b):
3) print("We are dividing",a,"with",b)
4) if b==0:
5) print("OOPS...cannot divide")
6) return
7) else:
8) return func(a,b)
9) return inner

10)

11) @smart_division
12) def division(a,b):
13) return a/b

14)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
without decorator we will get Error.In this case output is:

19)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
def num():

with decorator we won't get any error. In this case output is:

We are dividing 20 with 2

10.0

We are dividing 20 with 0

OOPS...cannot divide

None

Decorator Chaining

We can define multiple decorators for the same function and all these decorators will

form Decorator Chaining.

Eg:

@decor1

@decor

For num() function we are applying 2 decorator functions. First inner decorator will work

and then outer decorator.

Eg:

1) def decor1(func):
2) def inner():
3) x=func()
4) return x*x
5) return inner

6)

7) def decor(func):
8) def inner():
9) x=func()
10) return 2*x
11) return inner

12)

13) @decor1
14) @decor
15) def num():
16) return 10

17)

18) print(num())

Demo Program for decorator Chaining:

1) def decor(func):
2) def inner(name):
3) print("First Decor(decor) Function Execution")
4) func(name)
5) return inner

6)

7) def decor1(func):
8) def inner(name):
9) print("Second Decor(decor1) Execution")
10) func(name)
11) return inner

12)

13) @decor1
14) @decor
15) def wish(name):
16) print("Hello",name,"Good Morning")

17)

18) wish("Durga")

D:\durgaclasses>py decaratordemo1.py

Second Decor(decor1) Execution

First Decor(decor) Function Execution

Hello Durga Good Morning

🧠 Test Your Knowledge

5 Questions

Progress: 0 / 5
Keep Going!Python - Namespaces