Bird
0
0

Which of the following is the correct way to define a custom middleware class in Flask?

easy📝 Syntax Q12 of 15
Flask - Middleware and Extensions
Which of the following is the correct way to define a custom middleware class in Flask?
Aclass MyMiddleware: def __init__(self, app): self.app = app def __call__(self, environ, start_response): return self.app(environ, start_response)
Bdef MyMiddleware(app): return app
Cclass MyMiddleware: def handle_request(self): pass
Dclass MyMiddleware: def __init__(self): pass
Step-by-Step Solution
Solution:
  1. Step 1: Check middleware class structure

    Middleware must accept the app in __init__ and implement __call__ with environ and start_response.
  2. Step 2: Validate options

    Only class MyMiddleware: def __init__(self, app): self.app = app def __call__(self, environ, start_response): return self.app(environ, start_response) matches this pattern correctly. Others miss __call__ or parameters.
  3. Final Answer:

    Correct middleware class with __init__ and __call__ methods -> Option A
  4. Quick Check:

    Middleware class needs __call__ method [OK]
Quick Trick: Middleware class must implement __call__(environ, start_response) [OK]
Common Mistakes:
MISTAKES
  • Omitting __call__ method
  • Not passing app to __init__
  • Using function instead of class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes