+ 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?
6 odpowiedzi
+ 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.
0
Kandacewilliams110
0
Domin ready for
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
- 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