Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
The Singleton Pattern (python-patterns.guide)
3 points by guiambros on Aug 19, 2019 | hide | past | favorite | 1 comment


Excellent, 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.




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: