averainy's Blog

averainy

25 May 2025

Python Decorators Sample

Print a log of the start and end of method execution using python’s decorator

import logging

logging.basicConfig(level=logging.INFO)

def log(func):
    def wrapper(*args, **kwargs):
        logging.info(f"Method {func.__name__} start")
        result = func(*args, **kwargs)
        logging.info(f"Method {func.__name__} end")
        return result
    return wrapper


@log
def test():
    print('test')
    return 1


test()

Categories