As pointed out in the comments and another answer, you also need to define the next() method in your class to make it an iterator.
In addition, I notice that you are using yield instead of return . Because of which, executing next(lines) will not print the next line, but it will give you a generator object.
class linehistory: def __init__(self, lines, histlen=3): self.lines = lines self.history = deque(maxlen=histlen) def __iter__(self): return self def clear(self): self.history.clear() def next(self):
And then do the following:
>>> f = open("somefile.txt") >>> lines = linehistory(f) >>> next(lines) ... "Some line"
If you use yield instead of return , it will give a generator object:
>>> = open("somefile.txt") >>> lines = linehistory(f) >>> next(lines) ... <generator object next at 0xb4bb961c> >>>
xyres
source share