Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
yield from
#1
hi
in the below code, what is doing yield from? please explain:

#yield_from.py
# from:https://treyhunner.com/2018/04/keyword-arguments-in-python/


def join(*iterables, joiner):
    if not iterables:
        return
    yield from iterables[0]     #? yield from=?
    for iterable in iterables[1:]:
        yield joiner
        yield from iterable

'''That joiner variable doesn’t have a default value, so it must be specified:'''
list1=list(join([1, 2, 3], [4, 5], [6, 7], joiner=0))
print(list1)    # out: [1, 2, 3, 0, 4, 5, 0, 6, 7]
list2=list(join([1, 2, 3], [4, 5], [6, 7], joiner='-'))
print(list2)    #out: [1, 2, 3, '-', 4, 5, '-', 6, 7]
# the list(join([1, 2, 3], [4, 5], [6, 7])) causes erorr.with below message:
#TypeError: join() missing 1 required keyword-only argument: 'joiner'
thanks
Reply


Messages In This Thread
yield from - by akbarza - Apr-19-2024, 07:55 AM
RE: yield from - by Larz60+ - Apr-19-2024, 08:52 AM
RE: yield from - by deanhystad - Apr-19-2024, 01:22 PM
RE: yield from - by snippsat - Apr-19-2024, 02:44 PM
RE: yield from - by DeaD_EyE - Apr-19-2024, 07:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  yield usage as statement or expression akbarza 5 944 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Using list comprehension with 'yield' in function tester_V 5 1,360 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Yield generator weird output Vidar567 8 3,381 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Trying to access next element using Generator(yield) in a Class omm 2 2,051 Oct-19-2020, 03:36 PM
Last Post: omm
  Yield statement question DPaul 6 2,589 Sep-26-2020, 05:18 PM
Last Post: DPaul
  Problem about yield, please help!! cls0724 5 2,965 Apr-08-2020, 05:37 PM
Last Post: deanhystad
  does yield support variable args? Skaperen 0 1,714 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 4,351 Jun-04-2019, 10:26 PM
Last Post: snippsat
  yield help chakox 5 3,364 Apr-13-2019, 09:42 PM
Last Post: chakox
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 2,887 Mar-21-2019, 08:54 PM
Last Post: mmoelle1

Forum Jump:

User Panel Messages

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