Permalink
Browse files

decorator to memoize expensive functions (usable with "@Property")

  • Loading branch information...
stnbu committed Sep 7, 2018
1 parent 45af915 commit e2e0adb5f24b14d29e8674703d18a283629685a8
Showing with 15 additions and 0 deletions.
  1. +15 −0 mutils/memoized_property.py
@@ -0,0 +1,15 @@
# -*- mode: python; coding: utf-8 -*-
import functools
def memoized(f):
"""Decorate a function (without arguments), caching its results and returning the cache when available.
"""
proxy_name = '_' + f.__name__
@functools.wraps(f)
def wrapper(self):
if hasattr(self, proxy_name):
return getattr(self, proxy_name)
setattr(self, proxy_name, f(self))
return getattr(self, proxy_name)
return wrapper

0 comments on commit e2e0adb

Please sign in to comment.