Bird
0
0

You want to create a Flask app using the application factory pattern that supports multiple configurations (development and production). Which approach correctly applies this pattern?

hard📝 lifecycle Q15 of 15
Flask - Ecosystem and Patterns
You want to create a Flask app using the application factory pattern that supports multiple configurations (development and production). Which approach correctly applies this pattern?
Adef create_app(config_name): app = Flask(__name__) if config_name == 'dev': app.config.from_object('config.DevConfig') else: app.config.from_object('config.ProdConfig') return app
Bapp = Flask(__name__) if config_name == 'dev': app.config.from_object('config.DevConfig') else: app.config.from_object('config.ProdConfig')
Cdef create_app(): app = Flask(__name__) app.config = {} return app
Ddef create_app(config_name): app = Flask(__name__) app.config = config_name return app
Step-by-Step Solution
Solution:
  1. Step 1: Understand configuration loading in factory

    The factory should accept a parameter to select configuration and load it properly using app.config.from_object().
  2. Step 2: Evaluate options

    def create_app(config_name): app = Flask(__name__) if config_name == 'dev': app.config.from_object('config.DevConfig') else: app.config.from_object('config.ProdConfig') return app correctly uses a parameter and loads config classes conditionally inside the factory function.
  3. Step 3: Identify incorrect options

    app = Flask(__name__) if config_name == 'dev': app.config.from_object('config.DevConfig') else: app.config.from_object('config.ProdConfig') defines app globally, breaking factory pattern. Options C and D do not load config properly.
  4. Final Answer:

    def create_app(config_name): app = Flask(__name__) if config_name == 'dev': app.config.from_object('config.DevConfig') else: app.config.from_object('config.ProdConfig') return app -> Option A
  5. Quick Check:

    Factory with config param loads correct settings [OK]
Quick Trick: Pass config name to factory and load config inside [OK]
Common Mistakes:
MISTAKES
  • Creating app globally instead of inside factory
  • Assigning config directly instead of loading
  • Not using parameter to select config

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes