class CountingIterator(object):

    def __init__(self, it):
        self.it = it
self.count = 0

def __iter__(self):
    return self

def next(self):
    nxt = next(self.it)
self.count += 1
return nxt
__next__ = next
Python: Counting the items in a generator consumed by other code