Skip to content

laundmo/multiton

Repository files navigation

Multiton

pypi travis docs

A Multiton metaclass for preventing duplicate instances based on init values.

Features

  • 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.

Quickstart

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)

Credits

This package was created with Cookiecutter and the udreyr/cookiecutter-pypackage project template.

About

A Multiton metaclass for preventing duplicate instances based on init values.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published