Python Forum

Full Version: receive from a generator, send to a generator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i can receive a stream from some generator like:
    for thing in some_generator():
        ...
is there some equivalent to send to some generator?
It's called a co-routine.
David Beazley has a good course on same here: http://www.dabeaz.com/coroutines/
and a pycon video here:https://www.youtube.com/watch?v=MCs5OvhV9S4
i just want to know the "pythonic way" to send to a generator. that link to an article has a lot of code samples, but it is hard to review those and all the ones i looked at before i gave up were not sending to a generator. just post one example, if you know how to code it.
(Feb-02-2018, 03:57 AM)Larz60+ Wrote: [ -> ]i just want to know the "pythonic way" to send to a generator.
I don't think 'sending' to a generator is pythonic. You can as well write a small class an call a method instead of sending an item. It's conceptually simpler and more flexible. This is my humble opinion.
Gribouillis -- Actually Skaperen wrote ...
Quote:i just want to know the "pythonic way" to send to a generator.
the timestamp on Grib's quote is the time of Larz's post, so i'm guessing 2 quotes (1 intended) were being edited and it didn't get done all the way. i don't think there is a board bug here (though, i'm always looking for reasons to rewrite it into Python).

anyway, too bad there is no Pythonic way to send to a generator. i just hope people don't say my code is not Pythonic for that. oh wait, if they do then i can ask them how to make code to do what mine does that is Pythonic (as long as there is no way to get my results w/o sending to a generator ... such as writing a data compression program with a generator instead of a class ... BTDT but that was in C).
There is one. The example that I linked to in post #4 does just that.
I can be Pythonic and still ill advised.
(Feb-04-2018, 06:35 PM)wavic Wrote: [ -> ]https://docs.python.org/3/reference/expr...rator.send
yeah, but you have to make an ugly loop around that, instead of a pretty one that receiving lets you do. that's the way i have been doing it. i guess we have to accept it as being as pythonic as we can get.

basically, sending to a generator is not an iteration and python generators are not symmetric (if they were, yield and send would be the same thing).