If the function lookForSpecificLine
returns True
(aka, if the file "foo.txt" contains the targetLine
), how does Python know to close the file handle? Would the file "foo.txt" remain open?
def lines(filename):
with open(filename, encoding='utf-8') as file:
for line in file:
yield line
def lookForSpecificLine(targetLine):
for line in lines('foo.txt'):
if targetLine == line:
return True
return False
Add a close() method for generator-iterators, which raises GeneratorExit at the point where the generator was paused. If the generator then raises StopIteration (by exiting normally, or due to already being closed) or GeneratorExit (by not catching the exception), close() returns to its caller.
This is because generators, like all iterators, can be exhausted. Unless your generator is infinite, you can iterate through it one time only. Once all values have been evaluated, iteration will stop and the for loop will exit. If you used next() , then instead you'll get an explicit StopIteration exception.
A Python generator is a function that produces a sequence of results. It works by maintaining its local state, so that the function can resume again exactly where it left off when called subsequent times.
Python generators are a simple way of creating iterators. All the work we mentioned above are automatically handled by generators in Python. Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).
Your file will remain open as long as the generator object is alive. When the generator is garbage collected (at the end of the lookForSpecificLine
function, usually), Python will call close
on it, as part of the co-routine protocol described in PEP 342. The close
method causes Python to throw a GeneratorExit
exception into the generator's code at the place it was paused (just after the yield
statement). Since you don't catch that exception (as you usually shouldn't), it will break out of the loop and cause the with
statement to close the file.
Note that if lookForSpecificLine
was more complicated and there was some risk of it causing an exception (that would be caught at a higher level), things might not get cleaned up quickly. That's because the exception traceback will keep the function's stack frame alive, and so the generator will not be garbage collected immediately and the file will not be closed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With