Bird
0
0

Given this Flask code, what will be printed when accessing /?

medium📝 component behavior Q4 of 15
Flask - Middleware and Extensions
Given this Flask code, what will be printed when accessing /?
from flask import Flask
app = Flask(__name__)

@app.before_request
def before():
    print('Before request')

@app.route('/')
def home():
    print('Home route')
    return 'Hello'
ABefore request\nHome route
BHome route\nBefore request
COnly Home route
DOnly Before request
Step-by-Step Solution
Solution:
  1. Step 1: Understand the order of execution

    The before_request function runs before the route function.
  2. Step 2: Trace the print statements

    First, 'Before request' prints, then 'Home route' prints when / is accessed.
  3. Final Answer:

    Before request\nHome route -> Option A
  4. Quick Check:

    before_request runs before route = Before request\nHome route [OK]
Quick Trick: before_request runs before route functions [OK]
Common Mistakes:
MISTAKES
  • Assuming route prints before before_request
  • Thinking only one print runs
  • Confusing print order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes