Skip to content

Instantly share code, notes, and snippets.

@thisfred
Created August 22, 2018 16:53
Show Gist options
  • Save thisfred/2283d824ba49000cafc3c49ea9b93488 to your computer and use it in GitHub Desktop.
Save thisfred/2283d824ba49000cafc3c49ea9b93488 to your computer and use it in GitHub Desktop.
from itertools import izip
def main():
original = izip(range(10), range(10))
counts = [0]
def counting(wrapped):
for i in wrapped:
counts[0] += 1
yield i
for i in counting(original):
print(i)
print(counts[0])
main()
@thisfred
Copy link
Author

from itertools import izip

class Counter:

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

    def __iter__(self):
        return self

    def next(self):
        result = next(self.iterator)
        self.count += 1
        return result


def main():

    original = izip(range(10), range(10))

    counter = Counter(original)
    for i in counter:
        print(i)

    print(counter.count)

main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment