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 in Python by Arvind
• 2,960 points
81 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 by Neel
• 3,010 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 17, 2018 in Python by Johnathon
• 9,090 points
51 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 in Python by Shashank
• 1,350 points
71 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,670 points

reshown Aug 24, 2018 by Priyaj 87 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
• 56,960 points
42 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 405 views
0 votes
1 answer

What are generators in Python?

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

answered Jul 11 in Python by Neel
• 3,010 points
26 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 in Python by Neel
• 3,010 points
24 views