Analyze the Python import system's caching mechanism. How does `sys.modules` prevent redundant work when a module is imported multiple times in different parts of an application?

Python interview question for Advanced practice.

Answer

Python modules are Singletons by default. The Mechanism: When you run import mymodule, Python checks the dictionary sys.modules. 1. Hit: If 'mymodule' is already a key in sys.modules, Python simply returns the existing module object. It does not re-read the file or re-execute the code. This makes subsequent imports $O(1)$ operations. 2. Miss: If not found, Python finds the file, compiles it to bytecode, executes the module-level code to build the module object, adds it to sys.modules, and then returns it. Production Impact: This caching ensures that global state (like database connection pools defined at module level) is shared across the application. However, it also means side effects at the top level of a module run only once.

Explanation

You can 'reload' a module using importlib.reload(), but this is risky because existing objects will still refer to the old class definitions.

Related Questions