Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For Python generators, you can yield from called functions by using yield from as in this (quick, not stellar) example:

  def first():
      yield 1
      yield from second()
      yield 4

  def second():
      yield 2
      yield 3

  print(list(first())) # collects all the results and prints them
  # Output: [1, 2, 3, 4]
But yeah, it doesn't work on a direct function call you have to know it's going to return a generator (or an iterable, like if it returns a list):

  def something():
     yield from something_else()

  def something_else():
     return [1,2,3,4]


Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: