pyexperiment.utils.Singleton¶
Provides data structures for unique objects.
The Singleton class whose implementation is inspired by Tornado’s singleton (tornado.ioloop.IOLoop.instance()), can be inherited by classes which only need to be instantiated once (for example a global settings class such as pyexperiment.Config). This design pattern is often criticized, but it is hard to beat in terms of simplicity.
A variant of the Singleton, the DefaultSingleton provides an abstract base class for classes that are only instantiated once, but need to provide an instance before being properly initialized (such as pyexperiment.Logger.Logger).
The function delegate_singleton ‘thunks’ a Singleton so that calls to the singleton instance’s methods don’t need to be ugly chain calls.
Written by Peter Duerr (Singleton inspired by Tornado’s implementation)
- class pyexperiment.utils.Singleton.DefaultSingleton[source][source]¶
ABC for singleton that does not automatically initialize
If get_instance is called on an uninitialized DefaultSingleton, a pseudo-instance is returned.
Sub-classes need to implement the function _get_pseudo_instance that returns a pseudo instance.
- classmethod get_instance()[source][source]¶
Get the singleton instance if its initialized. Returns, the pseudo instance if not.