Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Return or yield from a function that calls a generator?

I have a generator generator and also a convenience method to it - generate_all.

def generator(some_list):
  for i in some_list:
    yield do_something(i)

def generate_all():
  some_list = get_the_list()
  return generator(some_list) # <-- Is this supposed to be return or yield?

Should generate_all return or yield? I want the users of both methods to use it the same, i.e.

for x in generate_all()

should be equal to

some_list = get_the_list()
for x in generate(some_list)
over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

You're probably looking for Generator Delegation (PEP380)

For simple iterators, yield from iterable is essentially just a shortened form of for item in iterable: yield item

def generator(iterable):
  for i in iterable:
    yield do_something(i)

def generate_all():
  yield from generator(get_the_list())

It's pretty concise and also has a number of other advantages, such as being able to chain arbitrary/different iterables!

over 4 years ago · Santiago Trujillo Report

0

return generator(list) does what you want. But note that

yield from generator(list)

would be equivalent, but with the opportunity to yield more values after generator is exhausted. For example:

def generator_all_and_then_some():
    list = get_the_list()
    yield from generator(list)
    yield "one last thing"
over 4 years ago · Santiago Trujillo Report

0

The following two statements will appear to be functionally equivalent in this particular case:

return generator(list)

and

yield from generator(list)

The later is approximately the same as

for i in generator(list):
    yield i

The return statement returns the generator you are looking for. A yield from or yield statement turns your whole function into something that returns a generator, which passes through the one you are looking for.

From a user point of view, there is no difference. Internally, however, the return is arguably more efficient since it does not wrap generator(list) in a superfluous pass-thru generator. If you plan on doing any processing on the elements of the wrapped generator, use some form of yield of course.

over 4 years ago · Santiago Trujillo Report

0

You would return it.

yielding* would cause generate_all() to evaluate to a generator itself, and calling next on that outer generator would return the inner generator returned by the first function, which isn't what you'd want.

* Not including yield from

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!