def memoized_method(func):
    """
    A decorator that performs memoization on methods. It stores the cache on the object instance itself.
    """

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        self = args[0]
        assert func.__name__ in dir(self), "memoized_method can only be used on method!"

        if not hasattr(self, '_MEMOIZED_CACHE'):
            cache = self._MEMOIZED_CACHE = {}
        else:
            cache = self._MEMOIZED_CACHE

        key = (func, ) + args[1:] + tuple(kwargs)
        ret = cache.get(key, None)
        if ret is not None:
            return ret
        value = func(*args, **kwargs)
        cache[key] = value
        return value

    return wrapper

    A decorator that performs memoization on methods. It stores the cache on the object instance itself.
    
tensorpack/utils/argtools.py