Advertisement
treyhunner

loop_tracker

Jun 25th, 2017
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. class loop_tracker:
  2.     """
  3.    Usage::
  4.        >>> iterator = loop_tracker(n**2 for n in range(10))
  5.        >>> sum(iterator)/len(iterator)
  6.        28.5
  7.    """
  8.     def __init__(self, iterable):
  9.         self.iterable = iterable
  10.         self.size = 0
  11.     def __iter__(self):
  12.         return self
  13.     def __next__(self):
  14.         item = next(self.iterable)
  15.         self.size += 1
  16.         return item
  17.     def __len__(self):
  18.         return self.size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement