Apr-06-2020, 05:05 PM
I am still new to Python and i come across the below sample from my textbook that confuses me quite a lot.
Then you reach Type Error.
However, i dun understand the next step when the
I can comprehend the logic in between. I thought once you reached TypeError then it's the end of this loop. Why it will lead back to
Thanks!
[u]def flatten(nested, indent=""): try: print(indent, "Going to iterate over", nested) for sublist in nested: print(indent, "Going to iterate over flattening of",sublist) for element in flatten(sublist, indent+" "): print(indent, "Yielding", element) yield element except TypeError: print(indent, "Type Error! Yielding", nested) yield nested[/u]The above code is to flatten a list. I understand how a list is flattened till you reach a number.
Then you reach Type Error.
However, i dun understand the next step when the
yield nestedthen jump to
for element in flatten.
I can comprehend the logic in between. I thought once you reached TypeError then it's the end of this loop. Why it will lead back to
for element in flatten.
Thanks!