Link copied to clipboard. Share away!

Dissmis

Published on 09 Mar 2020

**Decorators in Python are ****functions**** that add additional functionality to an already defined function without modifying it.** To add this functionality, a decorator function will take an already defined function as the argument.

Let us understand more about decorators with the help of the following examples.

A decorator can be written for a function with single or multiple arguments. Let us understand them one by one.

Consider the below-given simple program to subtract two numbers.

```
def sub(a, b):
print(a - b)
#main() function
sub(10, 5)
```

Output: 5

Suppose, if the user passes the arguments as 5 followed by 10, we will end up with a negative value which is not the required output. So we need to rewrite the function definition if the first input value is smaller than the second input value, such that the Python interpreter swaps those two values and then performs subtraction. After including these changes, our code looks like this.

```
def sub(a, b):if(a < b):
a, b = b, a
print(a - b)
#main() function
sub(5, 10)
```

Output: 5

This is easy, right? But what if you cant or dont need to change the existing function but still want to extend the functionality of that?

Here is where decorators come in handy. Using decorators, you can add the swapping logic to 'sub' function without even touching it. So, let us see how to write a decorator function for the 'sub' function.

```
#original function
def sub(a, b):
print(a - b)
#decorator
def dec_sub(func):#inner function in which the swapping functionality is addeddef inner(a, b):if(a < b):
a, b = b, a
func(a, b) #passes the swapped values to the function 'sub'return inner
#main() function
res = dec_sub(sub) #calling decorator function by passing the function 'sub' as an argument
res(5, 10)
```

Output: 5

**Execution order:**

Consider a function named num, which prints the given input number.

```
def num(a):
print(a)
```

If the user inputs a negative number, it will be printed as such. But, that is not the required output. So, we need to use a decorator to add one more functionality such that it checks the given input number, converts it to a positive value (if negative) and prints.

In the below-given example, 'dec_num' is the decorator function written to convert a negative value to a positive value.

```
#original function
def num(a):
print(a)
#decorator
def dec_num(func):#inner function in which the process of converting a negative to positive value is addeddef inner(a):if(a < 0):
a = -a
func(a) #passes the positive value of the given negative value to the function 'sub'return inner
#main() function
res = dec_num(num)
res(-10)
```

Output: 10

1) What are decorators in Python?

Decorators in Python are functions that add additional functionality to an already defined function without modifying it.

2) What is the input for the decorators in Python?

Input for a decorator is the user-defined function in which an additional functionality has to be added.

If you have any feedback about this
article and want to improve this, please write to **enquiry@faceprep.in**

×