Performance: Environment-based settings
This concept affects the app startup time and runtime configuration loading, impacting initial response speed and stability.
from pydantic import BaseSettings class Settings(BaseSettings): debug: bool = False database_url: str class Config: env_file = '.env' settings = Settings()
import os class Settings: DEBUG = os.getenv('DEBUG', 'false').lower() == 'true' DATABASE_URL = os.getenv('DATABASE_URL') settings = Settings()
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct os.getenv calls multiple times | 0 | 0 | 0 | [X] Bad |
| Pydantic BaseSettings single load | 0 | 0 | 0 | [OK] Good |
settings.debug?
class Settings(BaseSettings):
debug: bool = False
settings = Settings()
.env file content:
DEBUG=true
from pydantic import BaseSettings
class Settings(BaseSettings):
api_key: str
settings = Settings()
Assuming no environment variable API_KEY is set.Pass the test and this pattern moves unknown