Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class loop_tracker:
- """
- Usage::
- >>> iterator = loop_tracker(n**2 for n in range(10))
- >>> sum(iterator)/len(iterator)
- 28.5
- """
- def __init__(self, iterable):
- self.iterable = iterable
- self.size = 0
- def __iter__(self):
- return self
- def __next__(self):
- item = next(self.iterable)
- self.size += 1
- return item
- def __len__(self):
- return self.size
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement