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?