TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Python yield keyword explained

127 pointsby interroover 12 years ago

5 comments

akashshahover 12 years ago
Other amazing Python answers by the same guy:<p>What is a metaclass in Python - <a href="http://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python/6581949#6581949" rel="nofollow">http://stackoverflow.com/questions/100003/what-is-a-metaclas...</a><p>Understanding Python decorators - <a href="http://stackoverflow.com/questions/739654/understanding-python-decorators/1594484#1594484" rel="nofollow">http://stackoverflow.com/questions/739654/understanding-pyth...</a>
kevsamuelover 12 years ago
Hi guys, I'm the autor of the answers.<p>Thanks for the feedback. Because of comments like these, I decided to spend more time at training people for a living, and I'm loving it.<p>So don't underestimate the power of web comments, it can literally change the way people live.
评论 #5128240 未加载
arsover 12 years ago
A helpful hint on the name: Yield should be read like "Yields" as in "returns" or "produces".<p>Not Yield as in the common multithreading command to allow another thread to run.<p>PS. Anyone know why they chose such a confusing name?
评论 #5127785 未加载
jiggy2011over 12 years ago
What I find weird about the yield keyword is that it seems to effect the execution of code that comes before the statement, causing it not to execute until later.<p>For example:<p><pre><code> def createGenerator(): print "aaa" mygen = createGenerator() </code></pre> outputs:<p><i>aaa</i><p>Whereas<p><pre><code> def createGenerator(): print "aaa" yield mygen = createGenerator() </code></pre> Outputs nothing.
评论 #5126710 未加载
评论 #5127828 未加载
johnsonqqover 12 years ago
It's a coroutine. Not exactly revolutionary.
评论 #5125122 未加载
评论 #5125144 未加载
评论 #5125447 未加载