🚀
fast-api
1.basics

Decorators

Decorator is a function that takes another function and extends its behavior without explicitly modifying it. In Python, decorators are often used to add functionality to existing functions or methods.

def log(func):
    def wrapper():
        print(f" called {func.__name__}()")
        func()
    return wrapper
 
 
@log
def get_shipment():
    return {
        "message": "Hello World !",
    }

Decorator with arguments

def log(level):
    def decorator(func):
        def wrapper():
            print(f"[{level}] called {func.__name__}()")
            func()
        return wrapper
    return decorator
 
@log("INFO")
def get_shipment():
    return {
        "message": "Hello World !",
    }

© 2024 Driptanil Datta.All rights reserved

Made with Love ❤️

Last updated on Mon Oct 20 2025