Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Memoize(object):
- def __init__(self, f):
- self.f = f
- self.computed = {}
- def __call__(self, *args):
- xs = self.computed.get(args)
- if xs :
- return xs
- else :
- value = self.f(*args)
- self.computed[args] = value
- return value
- def unmemoize(self):
- return self.f
- def fact(n):
- print("computing", n)
- if n == 0:
- return 1
- else:
- return fact(n-1) * n
- fact = Memoize(fact)
- # fact(5)
- # fact(6)
- # ...
- fact = fact.unmemoize()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement