This is a micro-package, containing the single metaclass SingletonModuleMeta.
SingletonModuleMeta can be used to create Equinox modules that are
singletons, meaning that only one instance of the module can exist at any given
time.
pip install oncequinoxCreate a singleton Equinox module:
import equinox as eqx
from oncequinox import SingletonModuleMeta
class MySingletonModule(eqx.Module, metaclass=SingletonModuleMeta):
value: str = "this is a singleton module"
# Create the singleton instance
singleton1 = MySingletonModule()
# Attempt to create another instance
singleton2 = MySingletonModule()
print(singleton1 is singleton2) # TrueDifferent classes maintain separate singleton instances:
import equinox as eqx
from oncequinox import SingletonModuleMeta
class ConfigA(eqx.Module, metaclass=SingletonModuleMeta):
name: str = "A"
class ConfigB(eqx.Module, metaclass=SingletonModuleMeta):
name: str = "B"
config_a = ConfigA()
config_b = ConfigB()
print(config_a is config_b) # False
print(config_a.name) # A
print(config_b.name) # BArguments are only used for the first instantiation:
import equinox as eqx
from oncequinox import SingletonModuleMeta
class HasValue(eqx.Module, metaclass=SingletonModuleMeta):
value: int
def __init__(self, value: int):
self.value = value
v1 = HasValue(10)
print(v1.value) # 10
# Subsequent calls ignore arguments and return existing instance
v2 = HasValue(20)
print(v2.value) # 10
print(v1 is v2) # True