Performance: Environment-based configuration
This affects the app startup time and memory usage by controlling which settings and resources load depending on the environment.
import os env = os.getenv('FLASK_ENV', 'production') app.config.from_pyfile(f'config_{env}.py') # load only relevant config
app.config.from_pyfile('config.py') # single config file with all settings # app loads all settings regardless of environment
| Pattern | Config Load Time | Memory Usage | Startup Delay | Verdict |
|---|---|---|---|---|
| Single large config file | High | High | Longer startup | [X] Bad |
| Environment-specific config files | Low | Low | Faster startup | [OK] Good |
from_object to load config from a Python class.app.config.from_object('config.DevelopmentConfig') correctly loads the class named DevelopmentConfig from config module.class Config:
DEBUG = False
class DevelopmentConfig(Config):
DEBUG = True
app = Flask(__name__)
app.config.from_object(DevelopmentConfig)
print(app.config['DEBUG'])app = Flask(__name__)
app.config.from_envvar('MY_CONFIG')
FLASK_ENV. Which code snippet correctly implements this?os.getenv('FLASK_ENV') to get the current environment string.Pass the test and this pattern moves unknown