Allison is coding...

100 Days of Code: Day 54

Notes of 100 Days of Code: The Complete Python Pro Bootcamp.

Understand the @app.route() syntax: it’s a Python decorator function.

Decorator function: a function, typically applied using the @ symbol, which wraps another function to extend or modify its behavior without changing its source code.

# 1. Define the decorator
def my_decorator(original_function):
    
    # 2. Define the wrapper (the "sandwich" logic)
    def wrapper():
        print("Something is happening BEFORE the function is called.")
        
        original_function()  # Call the actual function
        
        print("Something is happening AFTER the function is called.")
        
    # 3. Return the wrapper
    return wrapper

# Applying the decorator using @
@my_decorator
def say_hello():
    print("Hello, World!")

# Now, when we call the function...
say_hello()

Output:


Something is happening BEFORE the function is called.
Hello, World!
Something is happening AFTER the function is called.