+ 1
Can yield and return be used in the same function?
3 ответов
+ 2
yield is a form of function return that saves the state or scope of the function for re-entry. With a return from a function, all values are lost on exit. Not sure how two would react, but should be mutually exclusive in well written code. hth To me this is just a fast object method shortcut..
+ 1
I wrote a little code to test this and was surprised I did not get any error. Try checking out my Code Playground, but I saw my (function/generator) return a pointer to the result of a yield each time.
- 1
Yes, they can, and when you do it, the value returned will be the "value" of the StopIteration exception after you exhaust your generator