Bird
0
0

Identify the error in this Flask app factory code using an extension:

medium📝 Debug Q6 of 15
Flask - Middleware and Extensions
Identify the error in this Flask app factory code using an extension:
from flask import Flask
from flask_mail import Mail

mail = Mail()

def create_app():
    app = Flask(__name__)
    mail = Mail(app)
    mail.init_app(app)
    return app
ARe-creating the Mail instance inside the factory causes multiple instances.
BCalling init_app after passing app to constructor is redundant but not an error.
CMissing app creation before Mail instance.
DNo error, code is correct.
Step-by-Step Solution
Solution:
  1. Step 1: Check extension instance creation

    The extension instance is created globally, but then recreated inside the factory, which is incorrect.
  2. Step 2: Understand consequences

    Re-creating inside factory causes multiple instances and breaks the pattern.
  3. Final Answer:

    Re-creating the Mail instance inside the factory causes multiple instances. -> Option A
  4. Quick Check:

    Multiple extension instances = Re-creating the Mail instance inside the factory causes multiple instances. [OK]
Quick Trick: Create extension once globally, don't recreate inside factory [OK]
Common Mistakes:
MISTAKES
  • Recreating extension inside factory
  • Calling init_app redundantly
  • Ignoring instance duplication issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes