+ 16

How works and how to use: yield from... 🐍 statement.

I'm asking for 'yield from...' not 'yield' ⁉️ 🐍 still surprises me⁉️

21st Nov 2019, 5:02 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
11 Réponses
+ 7
Another surprisingly complex topic, also waiting for me to explore. It seems, "yield from" is much more than simple chaining of generators. It is a bidirectional channel that lets you pass values to and from generators, as well as propagate error messages across functions automatically. The important topic to research is: coroutines. Good place to start: https://stackoverflow.com/questions/9708902/in-practice-what-are-the-main-uses-for-the-new-yield-from-syntax-in-JUMP_LINK__&&__python__&&__JUMP_LINK-3 http://www.dabeaz.com/coroutines/
21st Nov 2019, 8:43 PM
Tibor Santa
Tibor Santa - avatar
+ 8
Thanks a lot but I asked for the 🐍 'yield from...' instruction & not for the 🐍 'yield' instruction⁉️🤔 Tibor Santa Thx, I think, I'll be also interesting for you; PEP 380 -- Syntax for Delegating to a Subgenerator https://www.python.org/dev/peps/pep-0380/#proposal https://code.sololearn.com/cKSxI1t2eJh3/?ref=app
21st Nov 2019, 6:45 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 6
HonFu Right. It's not so easy⁉️🤔 As Tibor Santa said it's "Another surprisingly complex topic... "‼️😶🤕
22nd Nov 2019, 4:10 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 2
It's just like Mirielle🐶 [Inactive] said. Yield transforms a given function into a generator. Considering the code provided above, the first time you ask the function for output it will give you "1". The function gets frozen in that first yield statement. Thus, the second time you ask use the function, it will "unfreeze" and proceed until the next yield is reached. And so on. I hope I managed to help in something!
21st Nov 2019, 5:50 PM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar
+ 2
yield from seems to be a yield statement for iterables, where each item in an iterable will be separately be yielded. For example: yield from [1, 2, 3] would equal: yield 1 yield 2 yield 3 or actually: for temp in [1, 2, 3]: yield temp But that statement might have more meanings, because it would be weird that Python created a statement for a thing which can be easily done with 2 lines of code.
21st Nov 2019, 8:53 PM
Seb TheS
Seb TheS - avatar
+ 1
Yield statement changes a function into a generator function. Generator functuin behaves little different than an original function. Generator function returns a generator object, that works a little like a list, you can: check whether an item is in generator object by in operator. iterate through a generator object with for loop. transform it into another iterable. When used in for loop: for i in gen_func(): print(i) First: gen_func() gets normally called and for loop waits gen_func to yield its first value. Second: gen_func yields first value, call of gen_func gets paused. Third: the yielded value gets assigned to variable i and for loop runs its codeblock once and i gets printed. Fourth: the gen_func call continues from where it was paused and for loop waits for new value to be yielded. Fifth: Same cycle will continue until function call terminates, for loop breaks or other thing (such as an error) causes termination for the loop.
21st Nov 2019, 6:03 PM
Seb TheS
Seb TheS - avatar
+ 1
That doesn't explain anything though. The question is what exactly yield from does and where you make use of it.
22nd Nov 2019, 11:38 AM
HonFu
HonFu - avatar
+ 1
I personally haven't used that tool yet, somehow it didn't occur to me. A generator usually yields from the inside, right? So when you use subgenerators in a generator, instead of writing a loop and yielding every next of that subgenerator, you basically just pull everything out and pass it right on to the outside. And it seems you can also put stuff in with send. As I said, so far I haven't at any point thought: 'If there was just a way to...'
22nd Nov 2019, 4:17 PM
HonFu
HonFu - avatar
23rd Nov 2019, 5:06 PM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar