What is differenxe between return and yield ??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is differenxe between return and yield ???

24th Sep 2017, 3:38 AM
A J
A J - avatar
4 Answers
+ 6
24th Sep 2017, 3:55 AM
David Ashton
David Ashton - avatar
+ 4
It's matter of memory. Yield creates generator. Return do not. Generators are very useful when you have huge iterable objects. They do not stay in the memory ready for more after doing what they are suppose to do. Doesn't really matter when the object is for example few thousands of records long. Sometimes there maybe millions! Hire is a nice expenation https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do
24th Sep 2017, 7:54 AM
Leno
Leno - avatar
+ 3
Yield and return are kind of similar - but yield is for a generator. Basically (if I understand correctly) a generator only returns a value when requested, and it's only valid for a single use - it only exists for the line it's called on. Supposed to be good for situations where you handle large data sources or items without a finite length - but I haven't applied it anywhere myself.
24th Sep 2017, 3:57 AM
John D. Smith
John D. Smith - avatar
+ 1
hi
24th Sep 2017, 7:16 AM
Hariharan Selvaraj
Hariharan Selvaraj - avatar