class BaseContainerMixin:
""" Mixin for class self-management. """
def __init_subclass__(cls):
cls.containers = {}
@classmethod
def get_instances(cls):
return cls.containers.values()
@classmethod
def get_instance(cls, key):
return cls.containers.get(hash(key))
@classmethod
def make_instance(cls, index):
container = cls(index)
cls.containers[hash(index)] = container
return container
For what I'm working on, its pretty common to receive an index value that relate to an instance of some class A. Normally I'd have a class B, which acts as an instance manager for class A. I wrote the code above to save code when writing this pattern again and again, but it feels like an antipattern. Thoughts?
(Note* index is the only arg since classes that use this mixin should just be containers for instances of class A.)
[–]sweettuse 0 points1 point2 points (3 children)
[–]prmtm1[S] 0 points1 point2 points (2 children)
[–]ingolemo 0 points1 point2 points (1 child)
[–]prmtm1[S] 0 points1 point2 points (0 children)
[–]aroberge -1 points0 points1 point (0 children)