Python Generator Return Value Edge Case in Memphis

Another Python edge case I ran into while adding support in Memphis: generator return values. The return value doesn't show up in iteration (like the `list()` builtin), but it is surfaced via `StopIteration` when you advance the generator directly (using the `next()` builtin). A short example is below. I can't say I've ever encountered this as a user, but it was a fun one to discover while implementing it.

  • Python snippet showing the difference in generator return values during iteration versus the next builtin.

One would expect the list() builtin to not return a StopIteration exception in the output 😉 However, return value inside a generator does get stored in StopIteration.value - that's why you see it there.

Like
Reply

To view or add a comment, sign in

Explore content categories