functools.py 497 B

12345678910111213141516171819
  1. # def cache(f):
  2. # def wrapper(*args):
  3. # if not hasattr(f, '__cache__'):
  4. # f.__cache__ = {}
  5. # key = args
  6. # if key not in f.__cache__:
  7. # f.__cache__[key] = f(*args)
  8. # return f.__cache__[key]
  9. # return wrapper
  10. class cache:
  11. def __init__(self, f):
  12. self.f = f
  13. self.cache = {}
  14. def __call__(self, *args):
  15. if args not in self.cache:
  16. self.cache[args] = self.f(*args)
  17. return self.cache[args]