import tokenize class test(object): def __init__(self): self.lines = ["if a == 2:", "print 'hey'"] self.current = 0 def __call__(self): if self.current == len(self.lines): raise StopIteration line = self.lines[self.current] self.current += 1 return line t = test() for type, token, start, end, line in tokenize.generate_tokens(t): print(type, repr(token), start, end, line)