Bird
0
0

Given this middleware code, what will be printed when a request is made?

medium📝 component behavior Q13 of 15
Flask - Middleware and Extensions
Given this middleware code, what will be printed when a request is made?
class SimpleMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        print('Before app')
        response = self.app(environ, start_response)
        print('After app')
        return response

app = SimpleMiddleware(app)
ANo output printed
BOnly Before app printed
COnly After app printed
DBefore app printed, then After app printed
Step-by-Step Solution
Solution:
  1. Step 1: Understand __call__ method flow

    When a request comes, 'Before app' prints, then the wrapped app runs, then 'After app' prints.
  2. Step 2: Confirm both prints execute

    Both print statements are before and after calling the app, so both appear in order.
  3. Final Answer:

    Before app printed, then After app printed -> Option D
  4. Quick Check:

    Middleware prints before and after app = A [OK]
Quick Trick: Middleware prints before and after calling app [OK]
Common Mistakes:
MISTAKES
  • Thinking only one print runs
  • Assuming prints happen in reverse order
  • Forgetting __call__ runs on each request

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes