Bird
0
0

Identify the error in this factory function:

medium📝 Debug Q6 of 15
Flask - Ecosystem and Patterns
Identify the error in this factory function:
def create_app():
    app = Flask(__name__)
    db = SQLAlchemy(app)
    return app

app = create_app()
AFlask app must be created after SQLAlchemy initialization
BSQLAlchemy should be initialized outside and then bound inside factory
CThe factory function must not return the app
DThere is no error in this code
Step-by-Step Solution
Solution:
  1. Step 1: Recall extension initialization best practice

    Extensions like SQLAlchemy should be created outside the factory without app, then initialized with app inside.
  2. Step 2: Analyze the code

    Here, SQLAlchemy is initialized with app inside factory, which is not recommended for the factory pattern.
  3. Final Answer:

    SQLAlchemy should be initialized outside and then bound inside factory -> Option B
  4. Quick Check:

    Extension init best practice = B [OK]
Quick Trick: Create extensions outside, init inside factory [OK]
Common Mistakes:
MISTAKES
  • Initializing extensions inside factory directly
  • Creating app after extension init
  • Not returning app instance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes