I feel like using yield is just extra code, at least for the example given. Can anyone give me an exaple of a typical use of yield? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

I feel like using yield is just extra code, at least for the example given. Can anyone give me an exaple of a typical use of yield?

29th Jun 2016, 1:38 PM
Lucas
6 Answers
+ 6
I take "yield" as a way of telling a function to iterate/generate a list, and that's just what "yield" is used for: generators. "yield" doesn't actually "print" the list. It's only part of your function that computes/processes the list. Once you call that function and ask it to "print" it will print what it processed or yielded. Take the example in chapter "Functional Programming" - section "Generators" - first example. Run it in the playground. Replace "yield i" with "print(i)" and watch why you need to use "yield" and not "print". It will run that "while" loop immediately then create an error. INSTEAD of it only running when the function is called, which would happen if you use "yield" followed by the function as it does in the example. Mess around with that code and experiment.
29th Jun 2016, 9:59 PM
Dean
0
Kandacewilliams110
4th Jul 2016, 10:22 AM
Kandi Williams
Kandi Williams - avatar
0
Domin ready for
4th Jul 2016, 10:22 AM
Kandi Williams
Kandi Williams - avatar
0
i think the most important reason of yield is the optimization of memory usage and execution time. Yield only generates the data when you need it. There are times that you need to handle immense amounts of information but in the course of your algorithm, not all in an instant
3rd Mar 2017, 2:55 PM
Naim Star Pink
Naim Star Pink - avatar
- 5
Remember name for called upon oath text messages from me and I was trying to get to see you soon for some reason this good news
4th Jul 2016, 10:24 AM
Kandi Williams
Kandi Williams - avatar