What is singleton?
Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
How to implement singleton using module?
In python it is easy to implement singleton using module. Init a instance is all you need.
class xxx:
# implement your class
# only one instance
my_instance = xxx()
Then if you import my_instance on other files you can have a singleton instance. However, if you implement singleton by this way you should know the instance will init at the begining. In other words, there is no lazy loading.
Why this way can work?
It involves the import system in python. If a module has been imported , python will not import it again but will check if it be binded to another name.
The first place checked during import search is
sys.modules
. This mapping serves as a cache of all modules that have been previously imported, including the intermediate paths. So iffoo.bar.baz
was previously imported,sys.modules
will contain entries forfoo
,foo.bar
, andfoo.bar.baz
. Each key will have as its value the corresponding module object.During import, the module name is looked up in
sys.modules
and if present, the associated value is the module satisfying the import, and the process completes. However, if the value isNone
, then aModuleNotFoundError
is raised. If the module name is missing, Python will continue searching for the module.
So when you import your module, the init method will be invok only one time. Then you can say that it is a singleton here.