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 LoggerMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        print('Before request')
        response = self.app(environ, start_response)
        print('After request')
        return response

app.wsgi_app = LoggerMiddleware(app.wsgi_app)
AOnly After request printed
BOnly Before request printed
CNo output printed
DBefore request printed, then After request printed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze __call__ method

    Prints 'Before request', calls app, then prints 'After request'.
  2. Step 2: Understand call order

    Both prints happen on each request, before and after app call.
  3. Final Answer:

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

    Prints before and after app call [OK]
Quick Trick: Middleware __call__ runs code before and after app call [OK]
Common Mistakes:
MISTAKES
  • Thinking only one print runs
  • Confusing order of prints
  • Assuming prints happen outside __call__

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes