StopIteration的抛出问题



 def get_log_lines(log_file): 
    line = read_line(log_file) 
    while True:
        try:
            if complex_condition(line):
                yield line
            line = read_line(log_file)
        except StopIteration:
            raise

问题在于为什么要在read_line外面套一层try except,直接让read_line抛出不就好了么,像这样:


 def get_log_lines(log_file): 
    line = read_line(log_file) 
    while True:
        if complex_condition(line):
            yield line
        line = read_line(log_file)

python generator

seoris 9 years, 9 months ago

Your Answer