Course Content
What is Python?
Introduction of Python and Its setup
0/2
Control Statement
Control statements are used to control the flow of execution depending upon the specified condition/logic.
0/4
File Handling
File handling is an important component of any application. Python has multiple functions for creating, reading, updating, and deleting files.
0/2
Examples
Following are the examples of python scripts to try hands-on, you are ready to start your python journey.
0/7
Python
About Lesson

What is Decorators in Python?

A decorator in Python in any callable Python object that is used to modify a function or a class. It takes in a function, adds some functionality, and returns it.

Decorators are a very powerful and useful tool in Python since it allows programmers to modify/control the behavior of function or class. Decorators are usually called before the definition of a function you want to decorate.

There are two different kinds of decorators in Python:

  • Function decorators
  • Class decorators

Example

def test_decorator(func):
	def function_wrapper(x):
		print("Before calling " + func.__name__)
		res = func(x)
		print(res)
		print("After calling " + func.__name__)
	return function_wrapper

@test_decorator
def sqr(n):
	return n ** 2
sqr(54)

Output

Before calling sqr
2916
After calling sqr