Advertisement
mikhail_dvorkin

Iterator and Generator example

Nov 28th, 2016
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. import itertools
  2.  
  3. def fib(n):
  4.     a, b = 1, 1
  5.     for _ in range(n):
  6.         yield a
  7.         a, b = b, a + b
  8.     return "I'm tired"
  9.  
  10. class Fib:
  11.     def __init__(self):
  12.         self.a, self.b = 1, 1
  13.  
  14.     def __iter__(self):
  15.         return self
  16.  
  17.     def __next__(self):
  18.         c = self.a
  19.         self.a, self.b = self.b, self.a + self.b
  20.         return c
  21.  
  22. seq = itertools.islice(Fib(), 6)
  23. for x in itertools.combinations(seq, 3):
  24.     print(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement