{"id":699,"date":"2026-03-23T16:58:00","date_gmt":"2026-03-23T08:58:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=699"},"modified":"2026-07-15T17:02:22","modified_gmt":"2026-07-15T09:02:22","slug":"python-%e7%94%9f%e6%88%90%e5%99%a8","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/23\/python-%e7%94%9f%e6%88%90%e5%99%a8\/","title":{"rendered":"python \u751f\u6210\u5668"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u751f\u6210\u5668(Generator)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u751f\u6210\u5668\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Python\u8bc6\u522b\u5230\u8fd9\u4e2a\u51fd\u6570\u4e2d\u5305\u542byield\u7684\u5173\u952e\u5b57\uff0c\u56e0\u6b64\u8be5\u51fd\u6570\u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u3002<br>def simple_generator():<br> &nbsp; &nbsp;print(\"\u5f00\u59cb\")<br> &nbsp; &nbsp;yield 1<br> &nbsp; &nbsp;print(\"\u7ee7\u7eed\")<br> &nbsp; &nbsp;yield 2<br> &nbsp; &nbsp;print(\"\u7ed3\u675f\")<br> &nbsp; &nbsp;yield 3<br>\u200b<br>\u200b<br>g = simple_generator() # \u751f\u6210\u5668\u51fd\u6570\u8fd4\u56de\u7684\u662f\u751f\u6210\u5668\uff0c\u751f\u6210\u5668\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<br>print(next(g)) &nbsp;# \u5f00\u59cb<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 1<br>print(next(g)) &nbsp;# \u7ee7\u7eed<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 2<br>print(next(g)) &nbsp;# \u7ed3\u675f<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 3<br># print(next(g))  # StopIteration!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u5229\u7528\u751f\u6210\u5668\u8fd9\u79cd\u7b80\u6613\u5199\u6cd5\uff0c\u5feb\u901f\u5b8c\u6210\u8fed\u4ee3\u5668\u7684\u7f16\u5199\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def countdown(start):<br> &nbsp; &nbsp;\"\"\"\u751f\u6210\u5668\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;while start &gt;= 0:<br> &nbsp; &nbsp; &nbsp; &nbsp;yield start<br> &nbsp; &nbsp; &nbsp; &nbsp;start -= 1<br>\u200b<br>\u200b<br># \u8c03\u7528\u751f\u6210\u5668\u51fd\u6570\uff0c\u8fd4\u56de\u751f\u6210\u5668<br>cd = countdown(5)<br>print(type(cd)) &nbsp; &nbsp; &nbsp; # &lt;class 'generator'&gt;<br>\u200b<br>for n in cd:<br> &nbsp; &nbsp;print(n, end=\" \") &nbsp;# 5 4 3 2 1 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u751f\u6210\u5668\u7684\u7279\u70b9\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u60f0\u6027\u8ba1\u7b97<\/strong>\uff1a\u53ea\u5728\u9700\u8981\u65f6\u751f\u6210\u503c\uff0c\u4e0d\u5360\u7528\u5927\u91cf\u5185\u5b58<\/li>\n\n\n\n<li><strong>\u72b6\u6001\u4fdd\u5b58<\/strong>\uff1a\u6bcf\u6b21 <code>yield<\/code> \u540e\u6682\u505c\uff0c\u4e0b\u6b21\u4ece\u6682\u505c\u5904\u7ee7\u7eed<\/li>\n\n\n\n<li><strong>\u4e00\u6b21\u6027<\/strong>\uff1a\u548c\u8fed\u4ee3\u5668\u4e00\u6837\uff0c\u53ea\u80fd\u904d\u5386\u4e00\u6b21<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u94fe\u5f0f\u8c03\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def sub_generator():<br> &nbsp; &nbsp;yield 1<br> &nbsp; &nbsp;yield 2<br>\u200b<br>\u200b<br>def main_generator():<br> &nbsp; &nbsp;yield \"\u5f00\u59cb\"<br> &nbsp; &nbsp;for value in sub_generator():<br> &nbsp; &nbsp; &nbsp; &nbsp;yield value<br> &nbsp; &nbsp;yield \"\u7ed3\u675f\"<br>\u200b<br>\u200b<br>for value in main_generator():<br> &nbsp; &nbsp;print(value)<br># \u5f00\u59cb<br># 1<br># 2<br># \u7ed3\u675f<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4f7f\u7528<code>yield from<\/code> \u8bed\u6cd5\u7cd6\u7b80\u5316\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def main_generator():<br> &nbsp; &nbsp;yield \"\u5f00\u59cb\"<br> &nbsp; &nbsp;yield from sub_generator() &nbsp;# \u59d4\u6258\u7ed9\u5b50\u751f\u6210\u5668<br> &nbsp; &nbsp;yield \"\u7ed3\u675f\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u53d1\u9001\u6570\u636e<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u8c03\u7528\u751f\u6210\u5668\u7684<code>send<\/code>\u51fd\u6570\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5411\u751f\u6210\u5668\u53d1\u9001\u6570\u636e\uff0c\u8be5\u6570\u636e\u4f1a\u5bfc\u81f4<code>yield<\/code>\u7684\u8868\u8fbe\u5f0f\u8fd4\u56de\u5bf9\u5e94\u7684\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calculator():<br> &nbsp; &nbsp;total = 0<br> &nbsp; &nbsp;while True:<br> &nbsp; &nbsp; &nbsp; &nbsp;x = yield total &nbsp;# yield \u8fd4\u56de\u5f53\u524d\u603b\u6570\uff0c\u5e76\u63a5\u6536\u65b0\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;if x is None:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break<br> &nbsp; &nbsp; &nbsp; &nbsp;total += x<br>\u200b<br>calc = calculator()<br>print(next(calc)) &nbsp; &nbsp; &nbsp;# \u8f93\u51fa: 0 (\u542f\u52a8)<br>print(calc.send(10)) &nbsp; # \u8f93\u51fa: 10 (\u53d1\u900110\uff0c\u7d2f\u52a0\u540e\u8fd4\u56de)<br>print(calc.send(20)) &nbsp; # \u8f93\u51fa: 30<br>print(calc.send(5)) &nbsp; &nbsp;# \u8f93\u51fa: 35<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u4f3c\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u4f7f\u7528\u5706\u62ec\u53f7\uff0c\u8fd4\u56de\u751f\u6210\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u751f\u6210\u5668\u8868\u8fbe\u5f0f \u2014\u2014 \u60f0\u6027\u8ba1\u7b97\uff0c\u8282\u7701\u5185\u5b58<br>squares_gen = (x**2 for x in range(1000000))<br>\u200b<br>print(type(squares_gen)) &nbsp; # &lt;class 'generator'&gt;<br>\u200b<br># \u6309\u9700\u83b7\u53d6\u503c<br>print(next(squares_gen)) &nbsp; # 0<br>print(next(squares_gen)) &nbsp; # 1<br>print(next(squares_gen)) &nbsp; # 4<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u751f\u6210\u5668(Generator) \u751f\u6210\u5668\u51fd\u6570 \u53ef\u4ee5\u5229\u7528..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-699","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=699"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/699\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/699\/revisions\/700"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}