Python Tip #107 (of 365):
Use the walrus operator to simplify "while" loops.
This generator function reads a file in chunks:
def read_in_chunks(path):
with open(path, mode="rb") as file:
chunk = file.βread(8192)
while chunk:
yield chunk
chunk = file.βread(8192)
Notice that "chunk = file.βread(8192)" appears twice: once before the loop and once at the end.
This is a somewhat common pattern with "while" loops.
π§΅ (1/2)



