Skip to content

Instantly share code, notes, and snippets.

@whiteclover
Created July 13, 2013 10:37
Show Gist options
  • Save whiteclover/5990289 to your computer and use it in GitHub Desktop.
Save whiteclover/5990289 to your computer and use it in GitHub Desktop.
memozie python
class memoize:
# class as decorator
def __init__(self, function):
self.function = function
self.memoized = {}
def __call__(self, *args):
try:
return self.memoized[args]
except KeyError:
self.memoized[args] = self.function(*args)
return self.memoized[args]
@memoize
def fibonacci_memoized(n):
if n in (0, 1): return n
return fibonacci_memoized(n - 1) + fibonacci_memoized(n - 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment