cwchen

Closure demo in Python

Feb 4th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.32 KB | None | 0 0
  1. class make_fibonacci(object):
  2.     def __init__(self):
  3.         self.current = 0
  4.         self._next = 1
  5.  
  6.     def __call__(self):
  7.         fib = self.current
  8.         self.current, self._next = self._next, self.current + self._next
  9.         return fib
  10.  
  11. iterator = make_fibonacci()
  12.  
  13. for i in range(10):
  14.     num = iterator()
  15.     print num
Add Comment
Please, Sign In to add comment