Skip to content

Instantly share code, notes, and snippets.

@saintsGrad15
Created October 29, 2019 20:34
Show Gist options
  • Select an option

  • Save saintsGrad15/e92c49635c0e38ee341c9378c58f8e7d to your computer and use it in GitHub Desktop.

Select an option

Save saintsGrad15/e92c49635c0e38ee341c9378c58f8e7d to your computer and use it in GitHub Desktop.
A general form of a class instance cacher
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