Misunderstood python yield
Asked Answered
E

4

9

This code below works correct :

def file_gen(f_name):
    f = open(f_name)
    for line in f:
        yield line

gen_line = file_gen("foo.html")
gen_line.next() # '<!DOCTYPE>\n'
gen_line.next() # '<html> \n' 
gen_line.next() # ... next line in file 

But this function raises StopIteration. I don't understand why ?

def file_gen(f_name):
    f = open(f_name)
    line = f.readline()
    yield line

gen_line = file_gen('foo.html')
gen_line.next()  # '<!DOCTYPE>\n'
gen_line.next()  # StopIteration
Epicotyl answered 14/11, 2013 at 15:15 Comment(3)
What would you expect to happen?Carnassial
The second one is actually a syntax error in Python (you cannot indent the yield line)Kiosk
@Kiosk sorry , i correctedEpicotyl
B
9

You have:

def file_gen(f_name):
    f = open(f_name)
    line = f.readline()
    yield line

Notice line = f.readline() This only reads 1 line from the file.

Compare:

def g(x):
    li=range(x)
    yield li.pop()

print list(g(10))
# [9]

with this:

def g(x):
    li=range(x)
    while li:
       yield li.pop()

print list(g(10))
# [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

yield can only be called once with a particular object or expression. Once it is used by the receiver it must be regenerated. So you need a loop around reading each line of the file.

You can use your second (less readable) form this way:

def file_gen(f_name):
    f = open(f_name)
    while True:
        line = f.readline()
        if not line:
            break
        yield line

You need a loop to create the the items to yield. In your first case, for line in f: yield line is a loop.

I would rewrite your function this way:

def file_gen(f_name):
    with open(f_name) as f:
        for line in f:
            yield line
Brower answered 14/11, 2013 at 15:22 Comment(3)
@A.M.Sultanov: Because line = f.readline() only reads 1 line. You need to wrap that in some form of a loop -- either a while loop of a more readable for loopBrower
Or more specifically, you only call yield once, so the next call to next() hits the end of the generator.Kirstinkirstyn
@chepner: Yes -- that is a better way to state it.Brower
B
2

You get StopIteration on the second next() because you've only yielded one result. Did you mean to do this instead?

def file_gen(f_name):
    f = open(f_name)
    lines = f.readlines()
    for line in lines:
        yield line
Braunite answered 14/11, 2013 at 15:24 Comment(0)
Y
1

doesn't

line = f.readline()

only give you one line to yield? therefore iteration stops after that...

Yuhas answered 14/11, 2013 at 15:20 Comment(2)
but if use this form: file = open('foo.html') file.readline() file.readline() ... it doesn't stop on the second lineEpicotyl
When I try that it just gives me the second line instead of the first, then stops iteration on successive next() calls, as I would expect. Anyhow, seems like you've got to the bottom of your issue...Yuhas
N
0

strong text

def text_lines(path):
   textFile = open(path, "r")
   while True:
      line = textFile.readline()
      if line :
         yield line.strip()
      else:
         break
   textFile.close()
Nurmi answered 20/1, 2021 at 2:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.