Python Forum
receive from a generator, send to a generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
receive from a generator, send to a generator
#1
i can receive a stream from some generator like:
    for thing in some_generator():
        ...
is there some equivalent to send to some generator?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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
Reply
#3
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
simple example here: https://stackoverflow.com/questions/1930...on-purpose
Reply
#5
(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.
Reply
#6
Gribouillis -- Actually Skaperen wrote ...
Quote:i just want to know the "pythonic way" to send to a generator.
Reply
#7
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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
There is one. The example that I linked to in post #4 does just that.
I can be Pythonic and still ill advised.
Reply
#9
https://docs.python.org/3/reference/expr...rator.send
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(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).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 279 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  Receive Input on Same Line? johnywhy 8 610 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  problem in using generator akbarza 2 436 Nov-07-2023, 08:40 AM
Last Post: perfringo
  pipelined generator akbarza 10 1,328 Oct-31-2023, 12:45 PM
Last Post: Gribouillis
  coding a loot generator matt1984 1 525 Aug-16-2023, 05:08 AM
Last Post: Pedroski55
  Trying to make a password generator Jankofcik 1 504 Aug-06-2023, 01:22 PM
Last Post: deanhystad
  Is the following code returning a generator expression? quazirfan 8 1,534 Apr-11-2023, 11:44 PM
Last Post: quazirfan
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,018 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  prime numbers with iterator and generator cametan_001 8 1,773 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  Generator behaviour bla123bla 2 1,072 Jul-26-2022, 07:30 PM
Last Post: bla123bla

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020