Skip to content

Instantly share code, notes, and snippets.

@minghan minghan/memoize.py
Created Jul 7, 2011

Embed
What would you like to do?
Python Memoize Example
# Memoize Example
class Pak(object):
# Do not use @staticmethod here
def memoize(key):
def decfunc(f):
def newfunc(self):
if not hasattr(self, key):
setattr(self, key, f(self))
else:
print "retrieve from mem"
return getattr(self, key)
return newfunc
return decfunc
@memoize('c')
def getc(self):
print "computing"
return 1 + 1
p = Pak()
p.getc()
p.getc()
p.getc()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.