doc finished, just tweaking some stuff
[nltk_ontology_framework.git] / src / util / memoized.py
blobb2f719d50544d8e7254b3e12603c6c5c9e127af7
1 # This Python file uses the following encoding: utf-8
2 '''
3 Created on May 12, 2011
5 @author: mjacob
6 '''
8 import functools
10 class Memoized(object):
11 def __init__(self, f):
12 self.__f = f
13 self.__cache = {}
15 def __call__(self, *args):
16 key = args[1:]
17 if key in self.__cache:
18 return self.__cache[key]
19 else:
20 result = self.__f(*args)
21 self.__cache[key] = result
22 return result
24 def __repr__(self):
25 """Return the function's docstring."""
26 return self.func.__doc__
28 def __get__(self, obj, objtype):
29 """Support instance methods."""
30 return functools.partial(self.__call__, obj)