why do we use decorators in python

Best answer


Add functionality to an existing code

People also ask


  • What is a decorator in Python?

  • By definition, decorator is a function that extends the functionality of another function without explicitly modifying it. It makes the code shorter and meanwhile improve the readability. In this article, I will be sharing with you how we shall use the Python decorators.

  • How to decorate the class function in Python?

  • Beside decorating normal function, the decorator function can be also used to decorate the class function, for instance, the @staticmethod and @property are commonly seen in Python code to decorate the class functions.

  • How to chain decorators in Python?

  • Multiple decorators can be chained in Python. This is to say, a function can be decorated multiple times with different (or same) decorators. We simply place the decorators above the desired function. The order in which we chain decorators matter. If we had reversed the order as,

  • How to call a function from another function in decorator?

  • In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. 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. print(This is inside the function !!)

    Leave a Reply

    Your email address will not be published.