Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
A generator function pauses at each yield and resumes on the next request, keeping its local state; reading the file line by line through a chain of generators processes it with constant memory.
Calling a function containing yield returns a generator object without running the body. Each next() runs the code until the following yield, returns that value and freezes the function's local variables until the next call. For a huge file, iterate over the file object, which already yields lines lazily, and build a pipeline such as read_lines, then parse, then filter, each a generator consuming the previous one. Only one line is in memory at a time, and results can be aggregated incrementally. yield from delegates to a sub-generator, and generators can be closed early, which runs their finally blocks.