8 ms·
The Python yield keyword explained
- plesn 16y agoloved the bank example !
- markmywords 16y agoThank you for linking this. It may not be hot news or a trendy topic but it was a very good read and a good refresher :)
- iamelgringo 16y agoI'm glad you liked it. i've recently discovered the awesomeness of generators, and I'm starting to use them a lot in my code. I don't see them a lot in other people's code that I read, so I figure I'd spread the word a bit.
- rlander 16y agoAlthough I agree it was an interesting read and I do like this language feature, I think generators should be used with caution since they might detract from the code's readability (especially generator expressions). Some async libraries like dieselweb just use them too much for my taste.
- ntoshev 16y agoGenerators actually allow you to decouple code in more ways than normal functions and classes let you. See the "Why functional programming matters" paper, or the more practical presentation here: http://www.dabeaz.com/generators/Generators.pdf http://www.dabeaz.com/generators/Generators.pdf
- rlander 16y agoWow, excellent resource, definitely a must-read. However, I still stand by my opinion that there are more intuitive ways to control flow in python other than generator tricks.
- jscore 16y agoOne of the most thorough explanations I've seen on any question.
- igorgue 16y agoI have it on my favorites, love it!, best explanation ever!
- ramidarigaz 16y agoHis decorator explanation is also excellent.
- jscore 16y agoYep, agreed, another gem.
- litewulf 16y agoCan I get a link to the decorator explanation? A quick browse through his history didn't reveal much.
- tmitchell 16y agohttp://stackoverflow.com/questions/739654/understanding-python-decorators#1594484 http://stackoverflow.com/questions/739654/understanding-pyth...
- nocipher 16y agoIt's always nice when you finally understand "magic".
- MarkSweep 16y agoI found looking at the code that the C# compiler generates for its yield keyword to also be helpful for understanding this keyword. Source of an iterator example: http://gist.github.com/524972 http://gist.github.com/524972 What the compiler generates for the above example: http://gist.github.com/524974 http://gist.github.com/524974 The meat of it is in the MoveNext() function, where the original function is split up along the yield keywords into cases in a switch statement.