Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### current bpython session - make changes and save to reevaluate session.
- ### lines beginning with ### will be ignored.
- ### To return to bpython without reevaluating make no changes to this file
- ### or save an empty file.
- class Int(int):
- def __call__(self, val):
- return Int(self + val)
- num = Int(1)
- num(2)
- ### 3
- num(3)(4)(5)(6), 1 + 3 + 4 + 5 + 6
- ### (19, 19)
- class CachedFibonacci:
- def __init__(self):
- self._cache = {}
- def __call__(self, n):
- nth = None
- if n in self._cache:
- return self._cache[n]
- elif n in [1, 0]:
- nth = 1
- else:
- nth = self(n-1) + self(n-2)
- self._cache[n] = nth
- return nth
- fib = CachedFibonacci()
- fib(3)
- ### 3
- fib._cache
- ### {0: 1, 1: 1, 2: 2, 3: 3}
- fib(6)
- ### 13
- fib._cache
- ### {0: 1, 1: 1, 2: 2, 3: 3, 4: 5, 5: 8, 6: 13}
- ###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement