Return in generator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Return in generator

Why doesn’t return work in a generator function. In the code here, when randint returns 1, my function returns nothing, not even none. It is strange, but I guess it just yields an empty generator iterator in that case? https://code.sololearn.com/c0kHQ2sm3i8w/?ref=app

8th Nov 2021, 1:15 PM
Edward Finkelstein
Edward Finkelstein - avatar
3 Answers
+ 2
Here, you can read what will be happen on a return in a generator function. In short words: The call will not get the return value. The "normal" use of return in this case, is to use it w/o any argument. https://stackoverflow.com/questions/26595895/return-and-yield-in-the-same-function
8th Nov 2021, 3:55 PM
Coding Cat
Coding Cat - avatar
+ 1
Thank you coding cat
8th Nov 2021, 5:44 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
Edward Finkelstein you are welcome. It was also interesting for me, to find out what was going on here 😉
8th Nov 2021, 5:49 PM
Coding Cat
Coding Cat - avatar