简体   繁体   中英

Iterating through a text file using a for loop in python - why does this work?

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!

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".)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM