Problem Statement
Explain Python’s import caching and how to reload a module safely.
Explanation
Imports are cached in sys.modules. Later imports reuse the already loaded module object, which speeds up startup and avoids duplicate singletons. This also means code changes are not seen until a restart.
To reload during development, use importlib.reload on the module object. Be careful: existing references keep pointing to old objects. In production, prefer a fresh process or a clean import path.
Code Solution
SolutionRead Only
import importlib, mymod # edit mymod on disk mymod = importlib.reload(mymod)
