Bird
0
0

Which of the following is the correct way to register a before_request function in Flask?

easy📝 Syntax Q12 of 15
Flask - Middleware and Extensions
Which of the following is the correct way to register a before_request function in Flask?
A@app.before_request\ndef my_function(): pass
Bapp.before_request(my_function())
Capp.before_request = my_function()
Dapp.before_request = my_function
Step-by-Step Solution
Solution:
  1. Step 1: Recall Flask decorator syntax

    Flask uses decorators like @app.before_request above a function to register it as a before_request handler.
  2. Step 2: Analyze each option

    @app.before_request\ndef my_function(): pass uses the correct decorator syntax. app.before_request(my_function()) calls the function instead of decorating. Options C and D assign incorrectly.
  3. Final Answer:

    @app.before_request\ndef my_function(): pass -> Option A
  4. Quick Check:

    Use @app.before_request decorator = A [OK]
Quick Trick: Use @app.before_request decorator above the function [OK]
Common Mistakes:
MISTAKES
  • Calling the function instead of decorating
  • Assigning function instead of decorating
  • Missing the @ symbol for decorator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes