Dear all,

I've been struggling with the following problem, and I thought maybe there
is someone here in python-list who could shine some light on this.

Suppose we have a generator function `subgen`, and we want to wrap this in
another generator function, `gen`. For clarity, these are "full"
generators, i.e. they make use of .send, .throw, .close, etc. The purpose
of `gen` is to intercept the first value yielded by `subgen`, transforming
it in some form, and forwarding it on to the caller; after that, it should
just delegate to the rest of the generator iterator with `yield from`.

The problem is, the semantics of `yield from` expect a "fresh" generator
iterator, not a partially consumed generator. Indeed, the first value that
`yield from` will send to the iterator will be `None`, to start the
execution of the generator. But in this case we have already started the
generator, and to continue its execution we should really send whatever we
received after yielding the first value. However there's no way to specify
this with the `yield from` syntax.

A solution would be to re-implement the semantics of `yield from`, but with
an optional starting value (other than None) that will be sent to the
iterator as the initial value. However this means, as I said,
re-implementing the whole of `yield from`, i.e. handling .send(), .throw(),
.close(), etc., which is not ideal. So that would not be a good solution.
Am I asking for the impossible?

More details and an example in my original stackoverflow question:
https://stackoverflow.com/q/65369447/6117426

Best,
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to