What are decorators in Python

0 votes
I am new to Python. Recently I came across a concept called decorators in Python? I read about it but still not clear about it. Can someone explain it to me in simple words?
Jul 19, 2019 in Python by Arvind
• 3,050 points
1,244 views

1 answer to this question.

+1 vote

Python has an interesting feature called decorators to add functionality to an existing code. A decorator takes in a function, adds some functionality and returns it.

In Python, functions are the first class objects, which means that –

  • Functions are objects; they can be referenced to, passed to a variable and returned from other functions as well.
  • Functions can be defined inside another function and can also be passed as argument to another function.

Decorators are very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the behavior 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.

@gfg_decorator

def hello_decorator():

    print("Gfg")

  '''Above code is equivalent to -

  def hello_decorator():

    print("Gfg")

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.

answered Jul 19, 2019 by Neel
• 3,020 points

Related Questions In Python

0 votes
1 answer

what are "and" and "or" operators in Python?

AND - True if both the operands ...READ MORE

answered Apr 18, 2018 in Python by Johnathon
• 9,090 points
855 views
0 votes
2 answers

What are the types of dictionary in python?

There are 4 types of dictionary Empty Integer Mixed Dictionary with ...READ MORE

answered Feb 14, 2019 in Python by Shashank
• 1,370 points
1,019 views
+1 vote
3 answers

What are the ways of detecting outliners in Python

code from http://eurekastatistics.com/using-the-median-absolute-deviation-to-find-outliers  This uses the L1 distance ...READ MORE

answered Aug 24, 2018 in Python by eatcodesleeprepeat
• 4,710 points

reshown Aug 24, 2018 by Priyaj 1,350 views
+5 votes
1 answer

What are metaclasses in Python?

A metaclass instantiates and defines behavior for ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
859 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,438 views
0 votes
1 answer
0 votes
1 answer

What are generators in Python?

There are two terms involved when we ...READ MORE

answered Jul 11, 2019 in Python by Neel
• 3,020 points
726 views
0 votes
1 answer

What are the arguments of sorted() function in Python?

Sorted() sorts any sequence (list, tuple) and ...READ MORE

answered Jul 29, 2019 in Python by Neel
• 3,020 points
1,133 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP