[Python-ideas] A send() built-in function to drive coroutines (original) (raw)

Anthony Towns aj at erisian.com.au
Tue Feb 24 13:56:49 CET 2015


On 24 February 2015 at 21:00, Steven D'Aprano <steve at pearwood.info> wrote:

If I have a function which expects a primed coroutine, and you provide an unprimed one, we get an obvious error:

py> def handleprimed(co): ... for i in (1, 3, 5): ... y = co.send(i) ... return y ... py> handleprimed(runningsum()) # Oops, unprimed! Traceback (most recent call last): File "", line 1, in File "", line 3, in handleprimed TypeError: can't send non-None value to a just-started generator

​Why would you ever want to use that function "unprimed", though? Writing "next(rs)" or "rs.send(None)" just seems like busywork​ to me; it's not something I'd write if I were writing pseudocode, so it's something I wish python didn't force me to write in order to use that language feature...

​But... you could just use a decorator to make that go away, I think? As in having a decorator along the lines of:

def primed_coroutine(f): @wraps(f) def fprime(*args, **kwargs): co = f(*args, **kwargs) next(co) return co return fprime

so that you could write and use a coroutine just by having:

@primed_coroutine def runningsum(): total = 0 while True: total += (yield total)​

​ handle_primed(runningsum())

​At least, now that I've thought of it, that's how I plan to write any generator-based coroutines in future...

Cheers, aj​ -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150224/8c94f69b/attachment.html>



More information about the Python-ideas mailing list