+ 1
what does the yield fucntion do?
I am currently stuck on the sololearn python turtorial about generators and yield and all that stuff. I am very confused on what it does and why is it there? I placed a code on by bio about what im trying to understand. Please help me by explaining it to me like to a dummy
2 Answers
+ 18
Return sends a specified value back to its caller whereas Yield can produce a sequence of values.
Please Read This Article:
https://www.geeksforgeeks.org/use-yield-keyword-instead-return-keyword-JUMP_LINK__&&__python__&&__JUMP_LINK/
+ 1
A function with a yield statement becomed a generator with little different behavior.
When you call a generator, it creates a generator object, but the "function" content won't be executed yet.
The "function" content will be executed when you call next with it:
next(<generator object>)
And it will pause when any yield statement is reached and the yielded value will be returned.
When you call next again:
next(<generator object>)
the "function" will continue from the next line after the previous yield statement searching for another yield statement.
The advantage of this is that you can create an unlimited sized list like objects.
https://code.sololearn.com/cnvmH3xZ0TGp/?ref=app