What is Flask's g object?
Answer
The g object in Flask is a global namespace object available during the lifetime of a request for storing data that multiple functions in the same request might need. It is created fresh for every new request and discarded at the end. Common uses: storing the current authenticated user after JWT verification in a before_request function, caching a database connection for the request, or sharing a traced request ID. Access it: from flask import g; g.current_user = user. Read it later: user = g.get('current_user', None). The g object is thread/context safe — each request has its own g even in multi-threaded servers. Do not confuse it with the Flask application config (app.config) which persists across requests, or the session which persists across user browser sessions.
Previous
How does FastAPI handle request validation and error responses?
Next
What is CORS and how do you enable it in Flask and FastAPI?