Python Forum
how to make my function be a generator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to make my function be a generator
#1
it was my understanding that a function with a yield statement would be a generator. but, what if the function i want to be the generator is really calling another function that does the actual yield? how would it be known that the 1st one is to be a generator (when there is no yield statement coded in it)? how would it be known that the 2nd one is to not be a generator (even though it does have a yield statement to do yields on behalf of its caller)?
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
If I understand correctly -
fnA calls fnB which is a generator. It returns a value to fnA. fnA should then yield that value back to whoever called fnA. It is then a generator.
Missing something?
Reply
#3
pgmX calls fnA expecting a generator to be returned. i'll identify that generator as genA/ pgmX iterates genA. fnA runs and calls fnB which produces many items on order that genA should yield. fnB cannot invoke yield for fnA as part of genA because Python considers fnB to be a generator since fnB has the yield statement. calling fnB would create genB. so fnA cannot have fnB do yield for it. instead, fnB needs to be implemented to return items to let fnA yield them.

a common reason to create and call a function is having the same big code in many places. you implement a function with that big code and call the function in those many places, instead of many copies that same code. but if that big code includes a yield statement your expectations will not be met unless you realize this turns the function into a generator. also, if the code calling that function is itself a function whose callers expect a generator, moving all its yield statements into another function causes it to no longer be a function.
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
  Trying to make a password generator Jankofcik 1 546 Aug-06-2023, 01:22 PM
Last Post: deanhystad
  Tuple generator, and function/class syntax quazirfan 3 3,891 Aug-10-2021, 09:32 AM
Last Post: buran
  How to make global list inside function CHANKC 6 3,117 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  list call problem in generator function using iteration and recursive calls postta 1 1,916 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  basic random number generator in replace function krug123 2 2,045 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,423 Jun-24-2020, 04:05 AM
Last Post: deanhystad
  Generator function for even numbers mp3909 4 6,025 Mar-21-2020, 07:40 PM
Last Post: buran
  generator function that yield from a list buran 9 4,192 Jun-04-2019, 10:26 PM
Last Post: snippsat
  How to make a function for auto generated id sunnyarora 2 2,923 May-03-2019, 12:47 PM
Last Post: Yoriz
  Rewrite a function to make it work with 'bottle-pymysql' nikos 1 1,979 Feb-26-2019, 02:59 PM
Last Post: nikos

Forum Jump:

User Panel Messages

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