EDIT: this is a duplicate of How to properly ignore exceptions.
My code:
try:
y = next(x)
z = next(y)
f(y, z)
except StopIteration:
pass
What I'd like to write instead:
with x as y, y as z:
f(y, z)
However, that raises AttributeError: __enter__
.
If with
is not the appropriate idiom, is there a different idiomatic way to do what I'm doing without the explicit except StopIteration: pass
, or is there a way to wrap an iterator in a type that supports with
? The semantics should be exactly the same as in the first snippet.
I'm asking because for y in x
will not work in this context.