0

I have an object which defines the iterator interface and also contains a built in file reader (and uses its iterator interface as follows)

class MyIter(object):
    def __init__(path):
        self.file = open(path, "r")

    def next(self):
        line = self.file.next()
        return line

    def __iter__(self):
        return self

I call it as:

r = MyIter("path_to_file")
for item in r:
    print item

This prints the whole file upto the last line. My question is that I never had to add any check for EOF or check the length of the returned line to specify some end condition. Why does this work?

4
  • 2
    It works because it's just a proxy over a file iterator: docs.python.org/2/library/stdtypes.html#file.next Commented Oct 12, 2015 at 13:53
  • 2
    self.file.next() raises StopIteration when there is no more to iterate. That signals the iteration to end. Commented Oct 12, 2015 at 13:53
  • 1
    Note that you could just make the __iter__ implementation return self.file. Commented Oct 12, 2015 at 14:25
  • Ah yes, but my example was very minimal. I do parse the line in the file to return a tuple, but I did not mention it as it would be just a distraction to my main question. Commented Oct 12, 2015 at 14:48

3 Answers 3

5

The next() method returns the next input line, or raises StopIteration when EOF is hit. So it automatically handles the EOF limitations.

Sign up to request clarification or add additional context in comments.

Comments

4

Your next implementation calls file.next() which has its own EOF check. "This method returns the next input line, or raises StopIteration when EOF is hit when the file is open for reading"

Comments

2

I am not a Python expert but should you be referencing "item" in the for?

for item in r:
    print "item ==> ", item 

But, yes, the next() method is raising the StopIteration for you.

2 Comments

Yup, exceptions will be passed up the stack and will generally behave the same whether you called next yourself or called some code that called next. Imho this is a fundamental flaw in the design of the exception system. If you call some code in another module and that code leaks an exception, then all you will see is that your for-loop terminates. You will spend ages chasing this bug.
That's an interesting insight. However, the stack trace should help with the debugging?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.