fbpx

Explain the concept of decorators in Python and provide an example.

Certainly! Decorators in Python are a way to modify or extend the behavior of functions or methods without changing their actual code. They are denoted by the "@" symbol. Here's a simple example:

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()

 

# Dream job to realty