def read_large_file(file_handler, block_size=10000):
block = []
for line in file_handler:
block.append(line)
if len(block) == block_size:
yield block
block = []
# don't forget to yield the last block
if block:
yield block
with open(path) as file_handler:
for block in read_large_file(file_handler):
print(block)
I am reading this piece of code above written by another. For this line:
if len(block) == block_size:
yield block
block = []
Does the block=[]
have a chance to be executed? I had thought yield
is like a return
statement. Also, why is there an if block
checking?
yield
is a way to pause execution and resume it again from the next line. Place a print statement to check if it gets executed.block_size
. It might be better to useif len(block) >= block_size:
.