What are decorators?

In Python, decorators serve as essential functions that enable the addition of functionality to an already existing function without altering its structure. These decorators are denoted by the @decorator_name syntax in Python and are invoked in a bottom-up manner. Below is an example illustrating how decorators work correctly:

def decorator_lowercase(function):   # defining a Python decorator     def wrapper():         result = function()         result_lowercase = result.lower()         return result_lowercase     return wrapper @decorator_lowercase ## calling the decorator def intro():                    # Normal function     return 'Hello, I AM SAM' print(intro())

Output: ‘hello,i am sam’


Comments

Leave a Reply

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