Python Forum
Problem about yield, please help!!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem about yield, please help!!
#5
Thanks for your help!
While i am trying my very best to understand the logic behind...but my question remains there.

Perhaps let me try to elaborate my concern in a more detailed way.

My understanding of the code is that the below recursion will keep going until i reach the 'flat' layer, i.e. 1

for sublist in nested:
            for element in flatten(sublist):
At this point, the 'flat' layer cannot be used in for again because it is an integer. So a TypeError takes places.

In order to capture it, we have the below code
except TypeError:
        yield nested
In this case, yield nested will return 1.

Then i dun understand what happens next. As you suggested, i thought the code freezes right there and gives us 1. I have to use next() to return the next 'flat' layer.

Nonetheless, it seems to me that it is the
yield element
that gives us 1 instead of
yield nested
.

Sorry for bothering you!
Reply


Messages In This Thread
Problem about yield, please help!! - by cls0724 - Apr-06-2020, 05:05 PM
RE: Problem about yield, please help!! - by cls0724 - Apr-07-2020, 04:42 PM
RE: Problem about yield, please help!! - by cls0724 - Apr-08-2020, 03:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  yield from akbarza 4 378 Apr-19-2024, 07:58 PM
Last Post: DeaD_EyE
  yield usage as statement or expression akbarza 5 892 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Using list comprehension with 'yield' in function tester_V 5 1,325 Apr-02-2023, 06:31 PM
Last Post: tester_V
  Yield generator weird output Vidar567 8 3,341 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Trying to access next element using Generator(yield) in a Class omm 2 2,025 Oct-19-2020, 03:36 PM
Last Post: omm
  Yield statement question DPaul 6 2,566 Sep-26-2020, 05:18 PM
Last Post: DPaul
  does yield support variable args? Skaperen 0 1,705 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  generator function that yield from a list buran 9 4,311 Jun-04-2019, 10:26 PM
Last Post: snippsat
  yield help chakox 5 3,335 Apr-13-2019, 09:42 PM
Last Post: chakox
  Multiple calls to Python interpreter embedded in C++ application yield segmentation f mmoelle1 0 2,861 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