A Multiton metaclass for preventing duplicate instances based on init values.
- Free software: MIT license
- Documentation: https://multiton.readthedocs.io.
- Instanciate a class again and get the first instance with the same value back.
- Define which values count
- Supply callables to get the needed values from the argument.
from multiton import MultitonMetaFactory
class TestMultiton(metaclass=MultitonMetaFactory(0, (1, lambda x: x[1]), kw_b=None) ):
def __init__(self, a, b, kw_a=None, kw_b=None) -> None:
self.a = a
self.b = b
self.kw_a = kw_a
self.kw_b = kw_b
instance_a = TestMultiton(42, [1, 15, 42], kw_a="this is the first instance", kw_b=15)
instance_b = TestMultiton(42, [5, 15, 801], kw_a="this is the second instance", kw_b=15)
assert instance_a is instance_b
print(instance_b.kw_a)
This package was created with Cookiecutter and the udreyr/cookiecutter-pypackage project template.