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?✗ Incorrect
The
g object stores data only for the current request, not globally or permanently.How do you access the
g object in Flask?✗ Incorrect
You import
g from Flask with from flask import g and then use it.What happens to data stored in
g after the request ends?✗ Incorrect
Data in
g is cleared automatically after the request finishes.Which of these is a good use for
g?✗ Incorrect
g is ideal for request-specific data like a database connection.Why avoid using global variables instead of
g for request data?✗ Incorrect
Global variables can mix data between requests, causing bugs.
g keeps data separate per request.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.