Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Squares:
- def __init__(self, n):
- self.n = n
- def __iter__(self):
- return self.SquaresIterator(self.n)
- class SquaresIterator:
- def __init__(self, n):
- self.n = n
- self.i = 0
- def __iter__(self):
- return self
- def __next__(self):
- if self.i >= self.n:
- raise StopIteration
- result = self.i ** 2
- self.i += 1
- return result
- sq_class = Squares(5)
- list(sq_class)
- [0, 1, 4, 9, 16]
- # calling again will create a new iterator
- list(sq_class)
- [0, 1, 4, 9, 16]
- def squares(n):
- for i in range(n):
- yield i ** 2
- sq_gen = squares(5)
- list(sq_gen)
- [0, 1, 4, 9, 16]
- # calling again will return empty [] is OK, generator exhausted.
- list(sq_gen)
- []
- def Squares(n):
- def inner():
- return squares_gen(n)
- def squares_gen(n):
- for i in range(n):
- yield i ** 2
- return inner
- sq_closure = Squares(5)
- list(sq_closure())
- [0, 1, 4, 9, 16]
- # calling again the inner will create a new generator so it is not exhausted
- list(sq_closure())
- [0, 1, 4, 9, 16]
- def Squares(n):
- def inner():
- return squares_gen(n)
- def squares_gen(n):
- for i in range(n):
- yield i ** 2
- inner.__iter__ = lambda : squares_gen(n)
- return inner
- sq_closure = Squares(5)
- list(sq_closure)
- ---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-13-beb02e61ccfb> in <module>
- ----> 1 for i in sq:
- 2 print(i)
- TypeError: 'function' object is not iterable
Add Comment
Please, Sign In to add comment