Ligare.programming.patterns.singleton
Module containg Singleton metaclass
Classes
|
Singleton metaclass. |
|
- final class Ligare.programming.patterns.singleton.Singleton(cls_name: str, bases: tuple[Type[Any]], members: dict[str, Any])[source]
Singleton metaclass.
To create a new Singleton class, set the metaclass of your class to Singleton:
class Foo(metaclass=Singleton): pass
Behavior: By default, Singleton classes prevent attribute modifications. To allow attribute modifications, define an attribute named _block_change in the class and set it to False. Not defining this attribute, or setting any other value, will enforce the default behavior.
Warning: Setting _block_change = False disables attribute protection, and Singleton is not thread-safe in this configuration. Additionally, it is not possible to prevent the deletion of _class_ attributes.
Examples: The following classes are all equivalent:
class Foo(metaclass=Singleton): pass class Foo(metaclass=Singleton): _block_change = True class Foo(metaclass=Singleton): _block_change = None class Foo(metaclass=Singleton): _block_change = 123
To enable attribute modifications:
class Foo(metaclass=Singleton): _block_change = False