I was thinking about using Singleton pattern in my project, so I searched StackOverflow to find a pythonic way to implement it. I found this question with the accepted answer saying that "a module with functions (and not a class) would serve well as a singleton". On other hand the second answer had a suggestion to use the Borg pattern. For me using a module is a simple and straightforward solution, so I'd like to understand when using Borg is preferable.
A singleton and a module represent only one instance, for all the application lifetime. This instance is kept instantiated, even when it's not needed.
The borg pattern is about shared state. Each client class will create a new instance of the borg, but that instance will be disposed when it's not needed anymore - it is a much more elegant approach.
Beyond that, it is much easier to subclass or mock a borg.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With