yield from
 yield from akbarza Lumberjack Posts: 142 Threads: 68 Joined: Jul 2023 Reputation: Apr-19-2024, 07:55 AM 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 Larz60+ aetate et sapientia Posts: 11,886 Threads: 474 Joined: Sep 2016 Reputation: Apr-19-2024, 08:52 AM 'join' don't use python class names for your function name, it will overload the original definition. If at any future time you need the original definition of join, you will get the new definition instead. The yield is a part of a generator and yields the current iteration value. see: Generators Example: ```>>> def next_num(): ... numlist=[1,2,3,4,5,6,7,8] ... for x in numlist: ... yield x ... >>> for num in next_num(): ... print(num) ... 1 2 3 4 5 6 7 8 >>>``` Reply Posts: 6,310 Threads: 16 Joined: Feb 2020 Reputation: Apr-19-2024, 01:22 PM yield from is for yielding values produced by another generator. Instead of this ```for i in range(10): yield i```You can now do this: `yield range(10)`But this isn't just a shorthand way of writing a generator. The reasons for yield from are discribed in this pep https://peps.python.org/pep-0380/ Reply Posts: 7,112 Threads: 122 Joined: Sep 2016 Reputation: Apr-19-2024, 02:44 PM ```def my_join(*iterables, my_joiner=None): if not iterables: return #yield from iterables[0] # The same as the loop under for item in iterables[0]: yield item for iterable in iterables[1:]: yield my_joiner yield from iterable list1 = list(my_join([1, 2, 3], [4, 5], [6, 7], my_joiner=9999)) print(list1) list2 = list(my_join([1, 2, 3], [4, 5], [6, 7], my_joiner='***')) print(list2) # Now i have made a default argument so now dos this work list3 = list(my_join([1, 2, 3], [4, 5], [6, 7])) print(list3)`````````Output:[1, 2, 3, 9999, 4, 5, 9999, 6, 7] [1, 2, 3, '***', 4, 5, '***', 6, 7] [1, 2, 3, None, 4, 5, None, 6, 7``````yield from was new in Python 3.3. As see in code i have comment it out,and write same code as a loop Reply DeaD_EyE So-and-so of the Yard Posts: 2,026 Threads: 9 Joined: May 2017 Reputation: Apr-19-2024, 07:58 PM ```# removed the * in front of iterables print("Definition of generator-function") def join(iterables, joiner): print("First step of generator") # spliting iterable in first iterable and the rest first, *rest = iterables # yield each element from first yield from first # here the generator pauses until # next iteration print("Second step of generator") for iterable in rest: # yield joiner print("yield") yield joiner # yield each element from iterable from rest print("yield from") yield from iterable # example my_iterables = ( range(3), range(4), range(2), ) print("Calling generator-function") gen = join(my_iterables, "-") # generator print("Generator:", gen) print("Code inside the generator is not executed yet") print("Iterating over generator with list(gen)") print() result = list(gen) # <- this consumes the generator # Now the generator is empy print("Trying to consume gen again: ", list(gen)) print("But the generator is exhausted.") print("Example with a for-loop") print() for element in join(my_iterables, "-"): print(element, type(element))``` Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! Reply

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