0
0
Flaskframework~5 mins

G object for request-scoped data in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the g object in Flask?
The g object is used to store data during a single request. It helps share information between functions without using global variables.
Click to reveal answer
beginner
How long does data stored in g last?
Data in g lasts only for the current request. It is cleared automatically after the request ends.
Click to reveal answer
beginner
How do you store a value in the g object?
You assign a value like this: g.my_value = 42. This value can then be accessed anywhere during the request.
Click to reveal answer
intermediate
Why is g preferred over global variables for request data?
Because g is unique to each request, it avoids conflicts and data leaks between users or requests, unlike global variables.
Click to reveal answer
intermediate
Can you give an example use case for the g object?
A common use is to store a database connection during a request so multiple functions can use it without reopening connections.
Click to reveal answer
What does the Flask g object store?
AData for the current request only
BGlobal data shared across all requests
CUser session data stored permanently
DConfiguration settings for the app
How do you access the g object in Flask?
AUse <code>request.g</code>
BImport it from flask and use it directly
CUse <code>session.g</code>
DIt is a global variable without import
What happens to data stored in g after the request ends?
AIt is saved to a file
BIt is sent to the client
CIt persists for the next request
DIt is cleared automatically
Which of these is a good use for g?
AStoring app configuration
BSaving user login info permanently
CStoring a database connection for the request
DKeeping global counters
Why avoid using global variables instead of g for request data?
AGlobal variables can cause data conflicts between requests
BGlobal variables are faster
CGlobal variables are cleared after each request
DGlobal variables are only for configuration
Explain what the Flask g object is and why it is useful.
Think about how to share info safely during one web request.
You got /4 concepts.
    Describe a practical example of using the g object in a Flask app.
    Consider resources that need to be reused during one request.
    You got /4 concepts.