What is a decorator

A decorator in Python is a design pattern that allows you to modify the behavior of a function or class. Decorators are often used to add functionality to existing code in a clean and readable manner without altering the original code structure.

One common use of decorators is for logging, access control, caching, and more, where you might want to wrap an existing function with additional behavior.

Keywords: Python, Decorators, Design Pattern, Function Modification, Clean Code
Description: This content provides an overview of decorators in Python, explaining their purpose and utility in modifying the behavior of functions and classes while maintaining clean code practices.
# Example of a simple decorator in Python

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()  # This will show the output from both the decorator and the function
    

Keywords: Python Decorators Design Pattern Function Modification Clean Code