I have a class defined as
class Conditional_Singleton(object):
def __init__(self, condition=None):
self.initialization = some_func_of(condition)
Since the “initialization” step really takes a lot of time to run and the output could be huge, I would really like that the class not be recreated over and over again given the same input, but do so once the input changes. Any ideas?
This will save the dict of created instances in the class itself and automatically reuse them if the same condition is passed.
Edit: Just saw your comment. You need to find a good way to hash
conditionfor this to work, so unless it derives from a class that implements__hash__, you need to implement it yourself.Edit 2: Using
__new__instead of__init__for the singleton.