def instance_cache(func):
 func_name = func.__name__
 @functools.wraps(func)
 def _cache_guard(self, *args, **kwargs):
  cache_key = (func_name, args)
  if kwargs:
   kwarg_items = tuple(sorted(kwargs.items()))
   cache_key = (func_name, args, kwarg_items)
  result = self._instance_cache.get(cache_key)
  if (result is not None):
   return result
  result = func(self, *args, **kwargs)
  self._instance_cache[cache_key] = result
  return result
 return _cache_guard

def instance_cache ( func ) : func_name = func . __name__ @ functools . wraps ( func ) def _cache_guard ( self , * args , ** kwargs ) : cache_key = ( func_name , args ) if kwargs : kwarg_items = tuple ( sorted ( kwargs . items ( ) ) ) cache_key = ( func_name , args , kwarg_items ) result = self . _instance_cache . get ( cache_key ) if ( result is not None ) : return result result = func ( self , * args , ** kwargs ) self . _instance_cache [ cache_key ] = result return result return _cache_guard
cache the result of a method on a per instance basis .