3 ms·
The Singleton Pattern
- carapace 7y agoExcellent, modules as singletons is covered: > Modules are “singletons” in Python because import only creates a single copy of each module; subsequent imports of the same name keep returning the same module object. For example, when the Module Objects chapter of the Python/C API Reference Manual asserts that “Single-phase initialization creates singleton modules,” it means by a “singleton module” a module for which only one object is ever created. You have to be careful that every place that imports the module does so in such a way as to get the same (cached) module object from sys.modules, but this is pretty usually straightforward.