What is @ operator in Python?

PythonServer Side ProgrammingProgramming

@ symbol is used to define decorator in Python. Decorators provide a simple syntax for calling higher-order functions. By definition, a decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it.

we have two different kinds of decorators in Python:

  • Function decorators
  • Class decorators

 A decorator in Python is any callable Python object that is used to modify a function or a class. A reference to a function  or a class  is passed to a decorator and the decorator returns a modified function or class. The modified functions or classes usually contain calls to the original function.

@decorator
def f(argument):
….

will replace f by decorator(f): calling f(argument) is then equivalent to decorator(f)(argument). 

raja
Published on 06-Mar-2018 05:51:14
Advertisements