2

I'm currently learning how to create a spell checker in Python. In some tutorials I see something like the following:


def ReadDictionaryFile(dictionaryfilename):
    dictionarywords = []                       # stores words in a list
    inputfile = open(dictionaryfilename, "r")

    for line in inputfile:                     # iterate over the lines of the file
        word = line.strip()                    # whitespace removed
        dictionarywords.append(word)           # appends word to the list
    inputfile.close()
    return dictionarywords

But I don't understand how python knows to separate it out into lines.

In for line in inputfile:, "line" is just a variable, so what part of the code is actually telling it stop at \n?

Is there some built in feature in python where for loops iterating through text just start the next iteration when they encounter \n? I couldn't find any info on this...

Any help appreciated!

3
  • 2
    Yes, it's a built-in feature. Commented Dec 26, 2019 at 17:40
  • Explained more here Commented Dec 26, 2019 at 17:43
  • 1
    File objects are iterators Commented Dec 26, 2019 at 17:53

1 Answer 1

4

This works because the file object returned by open implements that behavior in its special "dunder" (double-underscore) __iter__ method. This is the method that is called when you implicitly iterate over an object in a for loop.

For example, consider this code:

class LineIterator:
    def __init__(self, contents):
        self.contents = contents

    def __iter__(self):
        yield from self.contents.splitlines()


it = LineIterator("""Hello
Foobar
Goodbye""")

for line in it:
    print("The line was", repr(line))

This prints out

The line was 'Hello'
The line was 'Foobar'
The line was 'Goodbye'

That for loop is exactly equivalent to the explicit version:

for line in iter(it):
    print("The line was", repr(line))

or the really, really explicit version:

for line in it.__iter__():
    print("The line was", repr(line))

The original version, as well as the one using iter(it), just call the __iter__ method. The standard library uses this pattern extensively, and you can use it in your own code to make objects behave as desired.

(The yield from x basically means "pass each element x up to the loop".)

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

1 Comment

To be specific, file objects are not just iterable, the are iterators (so they implement __next__)

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.