0% found this document useful (0 votes)
36 views

Python Decorators

Python decorators allow modifying the functionality of a function by wrapping it in another function. The outer wrapper function is called the decorator. Decorators take the original function as an argument and return a modified version. Decorators can add additional behavior to the decorated function. The @ symbol is used to decorate a function with a decorator, which is equivalent to calling the decorator and assigning it to the function.

Uploaded by

Manpreet Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Python Decorators

Python decorators allow modifying the functionality of a function by wrapping it in another function. The outer wrapper function is called the decorator. Decorators take the original function as an argument and return a modified version. Decorators can add additional behavior to the decorated function. The @ symbol is used to decorate a function with a decorator, which is equivalent to calling the decorator and assigning it to the function.

Uploaded by

Manpreet Kaur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ALWAYSINFOTECH LEARNING CENTRE

TOPIC : Python Decorators

TRE
In Python, a decorator is a design pattern
that allows you to modify the functionality

CEN
of a function by wrapping it in another
function.

NG
The outer function is called the decorator,
RN I
which takes the original function as an
argument and returns a modified version of
L EA
it.
CH

Before we learn about decorators, we need to


O TE

understand a few important concepts related


IN F

to Python functions. Also, remember that


everything in Python is an object, even
A YS

functions are objects.


Nested Function
ALW

We can include one function inside another,

known as a nested function. For example,


def outer(x):
def inner(y):
return x + y
return inner

add_five = outer(5)

TRE
result = add_five(6)

CEN
print(result) # prints 11

NG
# Output: 11
RN I
Here, we have created the inner() function
L EA
inside the outer() function.
CH

Pass Function as Argument


O TE

We can pass a function as an argument to


IN F

another function in Python. For Example,


A YS
ALW

def add(x, y):


return x + y
def calculate(func, x, y):
return func(x, y)

result = calculate(add, 4, 6)
print(result) # prints 10

TRE
CEN
In the above example, the calculate() function

NG
takes a function as its argument. While

RN I
calling calculate(), we are passing the add()

function as the argument.


L EA
CH

In the calculate() function, arguments: func,


O TE

x, y become add, 4, and 6 respectively.


IN F

And hence, func(x, y) becomes add(4, 6) which


A YS

returns 10.
ALW

Return a Function as a Value

In Python, we can also return a function as

a return value. For example,


def greeting(name):
def hello():
return "Hello, " + name + "!"
return hello

greet = greeting("MANPREET")

TRE
print(greet()) # prints "Hello, MANPREET!"

CEN
# Output: Hello, MANPREET!

NG
RN I
In the above example, the return hello
L EA
statement returns the inner hello() function.

This function is now assigned to the greet


CH
O TE

variable.
IN F

That's why, when we call greet() as a


A YS

function, we get the output.


ALW

Python Decorators
As mentioned earlier, A Python decorator is

a function that takes in a function and

returns it by adding some functionality.

In fact, any object which implements the

TRE
special __call__() method is termed callable.

CEN
So, in the most basic sense, a decorator is

NG
a callable that returns a callable.
RN I
Basically, a decorator takes in a function,
L EA
adds some functionality and returns it.
CH
O TE

def make_pretty(func):
def inner():
IN F

print("I got decorated")


A YS

func()
return inner
ALW

def ordinary():
print("I am ordinary")
# Output: I am ordinary

Here, we have created two functions:

that prints "I am ordinary"

TRE
● ordinary()

that takes a function as its

CEN
● make_pretty()

argument and has a nested function named

NG
inner(), and returns the inner function.
RN I
L EA
We are calling the ordinary() function
CH

normally, so we get the output "I am ordinary".


O TE

Now, let's call it using the decorator


IN F

function.
A YS

def make_pretty(func):
ALW

# define the inner function


def inner():
# add some additional behavior to
decorated function
print("I got decorated")
# call original function
func()
# return the inner function
return inner

TRE
# define ordinary function

CEN
def ordinary():
print("I am ordinary")

NG
RN I
# decorate the ordinary function
decorated_func = make_pretty(ordinary)
L EA
# call the decorated function
CH

decorated_func()
O TE
IN F

Output:
I got decorated
A YS

I am ordinary
ALW

In the example shown above, make_pretty() is a

decorator. Notice the code,


decorated_func = make_pretty(ordinary)

● We are now passing the ordinary() function

as the argument to the make_pretty().

● The make_pretty() function returns the

TRE
inner function, and it is now assigned to

CEN
the decorated_func variable.

NG
decorated_func()
RN I
Here, we are actually calling the inner()
L EA
function, where we are printing
CH
O TE

@ Symbol With Decorator


IN F

Instead of assigning the function call to a


A YS

variable, Python provides a much more


ALW

elegant way to achieve this functionality

using the @ symbol. For example,

def make_pretty(func):
def inner():

print("I got decorated")

TRE
func()

CEN
return inner

NG
RN I
L EA
@make_pretty
CH

def ordinary():
O TE

print("I am ordinary")
IN F
A YS
ALW

ordinary()

Output:
I got decorated
I am ordinary
Here, the ordinary() function is decorated

with the make_pretty() decorator using the

@make_pretty syntax, which is equivalent to

calling ordinary = make_pretty(ordinary).

TRE
CEN
NG
RN I
L EA
CH
O TE
IN F
A YS
ALW

You might also like