Decorator that adds arg to the wrapped function!

PHOTO EMBED

Tue Feb 14 2023 15:48:55 GMT+0000 (Coordinated Universal Time)

Saved by @taha #python #decorator

def async_session(func):
    async def wrapper(*args, **kwargs):
        async with get_async_session() as session:
            return await func(session, *args, **kwargs)

    return wrapper
content_copyCOPY

Now the following will be correct: @async_session async def my_func(session): await session.execute(...) No need to import "session" or pass it as an arg when calling my_func!