Menu Close

Decorators in Python

Decorators in Python

In this article, you will learn all about the most important topic is decorator in Python. Decorators in Python is most famous and important because it provides the functionality of the existing Python code without changing the code.

Sometimes we have a function and we want to increase their functionality without a coding change in that function, So at that time decorator comes into the picture because the use of a decorator can increase the functionality of the existing function without altering the code.

Function in Python is the first-class object, Which means the function in Python can be used or passed as a parameter.

Here we will see some interesting facts about Python function before going through decorators in Python.

Assign function to the variable

You can assign the Python function to the variable and use that variable as a function.
Let’s see how can I do that.

Example:

def Addition(x, y):
	print(f"Addition of {x} and {y} is:- {x + y}")


#without assigning a function to the variable.
Addition(12, 34)

#Assign a function to the variable.
x = Addition
x(12, 30)

Output

The addition of 12 and 34 is:- 46
The addition of 12 and 30 is:- 42

Defining function inside another function

In Python, You can also define a function inside another function.

Example:

def Addition(a, b):
	def sum():
		return f"Addition of {a} and {b} is:- {a + b}"		
	return sum

result = Addition(20, 30)
print(result())

Output

The addition of 20 and 30 is:- 50

Passing function as an argument to the function

The function can also be passed inside another function as an argument.

Example:

def Message(msg):
	return msg()
		
def welcome():
	print("Welcome to the programming funda")
	
Message(welcome)

Output

Welcome to the programming funda

Now, after learning all the above concepts you are able to go with Python decorators.

What are decorators in Python?

Decorators in Python are a powerful tool that allows Python programmers to change the behavior of the existing Python function or class without changing the function and class code.

The decorator takes a function as an argument and adds some functionality and returns it.

Python Decorators Examples

Here we will see some examples of decorators in Python so that you don’t have any confusion regarding Python decorators.

Example 1:

def smart(any_function):
    def inner():
        print("Before execution")
        any_function()
        print("After execution")

    return inner

def func2():
    print("Inside the function")

var = smart(func2)
var()

Output

Before execution
Inside the function
After execution

You can use @decoratorname to use a decorator.

def smart(any_function):
    def inner():
	
        print("Before execution")
        any_function()
        print("After execution")

    return inner

@smart
def func2():
    print("Inside the function")
func2()

Output

Before execution
Inside the function
After execution

Here we will create a decorator that is capable of converting lower case to upper case.

def upper_case(myfunc):
	result = myfunc()
	print(result.upper())
	

@upper_case		
def message():
	return "Welcome to Programming Funda Python tutorial."

Output

WELCOME TO PROGRAMMING FUNDA PYTHON TUTORIAL.

A function return value

In the above example, the function does not return any value but here we will see what happens if a function returns some value.

def smart(any_function):
    def inner(*args, **kwargs):
	
        print("Before execution")
        return_value = any_function(*args)
        print("After execution")
        return return_value

    return inner

@smart
def func2(a, b):
    print("Inside the function")
    return a + b

	
print(f"Sum :- {func2(12, 23)}")

When you run the above program you will get output like:

Before execution
Inside the function
After execution
Sum:- 35

Conclusion

So In this article, you have learned all about Decorators in Python along with examples. Decorators in Python play the most major role when you want to change the behavior of the existing Python code without altering that code.

If this article genuinely helps you, please share and keep visiting for further Python tutorials.

For more information:- Click Here

Types of Inheritance in Python
How to use Class Decorator in Python

Related Posts