Elektrine lite

← Feed

@treyhunner@mastodon.social

Post #1350650

2026-04-17 18:10 UTC

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) #Python #DailyPythonTip

Replies (0)

No replies.