What is Decorator in python ?

Answer : A decorator in Python is a function that takes another function as an argument…

Decorator in python

  • A decorator in Python is a function that takes another function as an argument, while not changing the function being used as an argument.
  • Decorators dynamically alter the functionality of a function without using sub classes.
  • This is useful when you want to extend the functionality of functions and don’t want to modify them.
Python Decorators

Example:

[pastacode lang=”markdown” manual=”def%20decorator(func)%3A%0A%09def%20wrapper()%3A%0A%09%09print%20%22Hello%20Wikitechy%22%0A%09%09print%20func(%22Hai%22)%0A%09%09print%20%22How%20r%20you%3F%22%0A%20%0A%09return%20wrapper%0A%20%0Adef%20some_random_function(hello)%3A%0A%09return%20hello%0A%20%0Adecorated_function%20%3D%20decorator(some_random_function)%0Adecorated_function()” message=”” highlight=”” provider=”manual”/]
Leave a Reply

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

You May Also Like