Complete the code to import the Flask global object used for request-scoped data.
from flask import [1]
request instead of g.session which is for user sessions, not request data.The g object in Flask is used to store data during a request. Import it from flask.
Complete the code to store a value in the Flask g object during a request.
from flask import g def before_request(): g.[1] = 'user123'
session_id which is not typically stored in g.app_name.You can add any attribute to g. Here, user_id is a common example to store user info during a request.
Fix the error in accessing the g object attribute inside a view function.
from flask import g @app.route('/') def index(): user = g.[1] return f"Hello, {user}!"
userId or User_id.userid.Attribute names in g are case-sensitive. If you stored user_id, you must access it exactly as g.user_id.
Fill both blanks to correctly check if an attribute exists in g and use it.
from flask import g @app.route('/profile') def profile(): if hasattr(g, '[1]'): user = getattr(g, '[2]') else: user = 'Guest' return f"Profile: {user}"
hasattr and getattr.g.To safely access an attribute in g, check with hasattr and then get it with getattr using the same attribute name.
Fill both blanks to create a dictionary comprehension that stores lengths of strings in g if length is greater than 3.
from flask import g words = ['apple', 'bat', 'carrot'] g.lengths = {word: len(word) for word in words if len(word) [1] 3 and 'a' [2] word}
= instead of : in dictionary comprehension.== instead of > for length comparison.== instead of in to check substring presence.The dictionary comprehension needs a colon : between key and value, a greater than sign > to check length, and in to check if 'a' is in the word.