Skip to content

Instantly share code, notes, and snippets.

@apua
Created December 15, 2015 08:32
Show Gist options
  • Select an option

  • Save apua/5312a19fa336b16bac7c to your computer and use it in GitHub Desktop.

Select an option

Save apua/5312a19fa336b16bac7c to your computer and use it in GitHub Desktop.
import functools
# decorator for method?!
class Cache(object):
def __init__(self, func):
self.func = func
self.cache = {}
def __call__(self, *args):
if args not in self.cache:
self.cache[args] = self.func(*args)
return self.cache[args]
def __repr__(self):
return self.func.__doc__
def __get__(self, obj, objtype):
return functools.partial(self.__call__, obj)
cache = Cache
class C(object):
@cache
def main(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment