Decorators in Python Tutorials point


A decorator takes in a function, adds some functionality and returns it. Decorators are very powerful and useful tool in Python since it allows programmers to modify the behaviour of function or class. Decorators allow us to wrap another function in order to extend the behaviour of wrapped function, without permanently modifying it.

In Decorators, functions are taken as the argument into another function and then called inside the wrapper function.

Decorators In Python Tutorials Point
Decorators In Python Tutorials Point

Also see : python numpy

A decorator in python is any callable python object that is used to modify a function or class.They help to make our code shorter
There are two types of decorator in python: one is function decorator and another is class decorator.
a decorator is called before defining a function.
It takes a function and inserts some new functionality in it without changing the function itself. A reference to a function is passed to a decorator and the decorator returns a modified function. The modified function usually contain calls to the original function.

Syntax for Decorator in python tutorialspoint :


def hello_decorator(): 



def hello_decorator(): 

hello_decorator = gfg_decorator(hello_decorator)

In the above code, gfg_decorator is a callable function, will add some code on the top of some another callable function, hello_decorator function and return the wrapper function.

This is also called metaprogramming because a part of the program tries to modify another part of the program at compile time. Basically, a decorator takes in a function, adds some functionality and returns it.

Also see : seaborn for data visualization

Generally , we decorate a function and reassign it as,

Ordinary = make_pretty(ordinary)

This is a common construct and for this reason, Python has a syntax to simplify this. We can use the @ symbol along with the name of the decorator function and place it above the definition of the function to be decorated.

Decorating Functions with Parameters – If we had functions that took in parameters like:

Def divide(a,b):

Return a/b

The function has 2 parameters a and b. We know it will give an error if we pass b as 0. Now lets make a decorator to check for this case that will cause an error.

Def decorator_divide(func):

	Def inner(a,b):

		If b==0:

			Print(“cannot be divided”)


		return func(a,b)

	return inner


def divide(a,b):


This new implementation will return None if the error condition arises.

Summary :

In this article we saw Decorators In Python Tutorials Point so about this articleyou have any query then free to ask me

Also read : python matplotlib

Name of Intern who share this Task :

Charul gupta

My name is Charul Gupta have applied for internship via Internshala.Here is my link and copy of the task .link for task-

I am Mr. Sachin pagar the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

google ads

Pythonslearning protected By DMCA