Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda