下面的代码可以正常工作:
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
但是这个功能提高了StopIteration
。我不明白为什么?
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