def memoize_when_activated(fun):
 @functools.wraps(fun)
 def wrapper(self):
  if (not wrapper.cache_activated):
   return fun(self)
  else:
   try:
    ret = cache[fun]
   except KeyError:
    ret = cache[fun] = fun(self)
   return ret
 def cache_activate():
  'Activate cache.'
  wrapper.cache_activated = True
 def cache_deactivate():
  'Deactivate and clear cache.'
  wrapper.cache_activated = False
  cache.clear()
 cache = {}
 wrapper.cache_activated = False
 wrapper.cache_activate = cache_activate
 wrapper.cache_deactivate = cache_deactivate
 return wrapper

def memoize_when_activated ( fun ) : @ functools . wraps ( fun ) def wrapper ( self ) : if ( not wrapper . cache_activated ) : return fun ( self ) else : try : ret = cache [ fun ] except KeyError : ret = cache [ fun ] = fun ( self ) return ret def cache_activate ( ) : wrapper . cache_activated = True def cache_deactivate ( ) : wrapper . cache_activated = False cache . clear ( ) cache = { } wrapper . cache_activated = False wrapper . cache_activate = cache_activate wrapper . cache_deactivate = cache_deactivate return wrapper
a memoize decorator which is disabled by default .