Created
October 29, 2019 20:34
-
-
Save saintsGrad15/e92c49635c0e38ee341c9378c58f8e7d to your computer and use it in GitHub Desktop.
A general form of a class instance cacher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ClassInstanceCacher(object): | |
| # Rename the outer class at will. | |
| # Implement Klass to suit use case | |
| __cache = {} | |
| def __new__(cls, _id): | |
| if cls.__cache.get(_id) is None: | |
| cls.__cache[_id] = cls.Klass(_id) | |
| return cls.__cache[_id] | |
| class Klass(object): | |
| def __init__(self, _id): | |
| self._id = _id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment