{"id":568,"date":"2025-10-23T15:13:59","date_gmt":"2025-10-23T07:13:59","guid":{"rendered":"http:\/\/www.pyteam.cn\/?p=568"},"modified":"2025-10-23T15:13:59","modified_gmt":"2025-10-23T07:13:59","slug":"python3-%e8%bf%ad%e4%bb%a3%e5%99%a8%e4%b8%8e%e7%94%9f%e6%88%90%e5%99%a8","status":"publish","type":"post","link":"http:\/\/www.pyteam.cn\/index.php\/2025\/10\/23\/python3-%e8%bf%ad%e4%bb%a3%e5%99%a8%e4%b8%8e%e7%94%9f%e6%88%90%e5%99%a8\/","title":{"rendered":"Python3 \u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8fed\u4ee3\u5668<\/h2>\n\n\n\n<p>\u8fed\u4ee3\u662f Python \u6700\u5f3a\u5927\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u662f\u8bbf\u95ee\u96c6\u5408\u5143\u7d20\u7684\u4e00\u79cd\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u53ef\u4ee5\u8bb0\u4f4f\u904d\u5386\u7684\u4f4d\u7f6e\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u8fed\u4ee3\u5668\u5bf9\u8c61\u4ece\u96c6\u5408\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\u8bbf\u95ee\uff0c\u76f4\u5230\u6240\u6709\u7684\u5143\u7d20\u88ab\u8bbf\u95ee\u5b8c\u7ed3\u675f\u3002\u8fed\u4ee3\u5668\u53ea\u80fd\u5f80\u524d\u4e0d\u4f1a\u540e\u9000\u3002<\/p>\n\n\n\n<p>\u8fed\u4ee3\u5668\u6709\u4e24\u4e2a\u57fa\u672c\u7684\u65b9\u6cd5\uff1a<strong>iter()<\/strong>&nbsp;\u548c&nbsp;<strong>next()<\/strong>\u3002<\/p>\n\n\n\n<p>\u5b57\u7b26\u4e32\uff0c\u5217\u8868\u6216\u5143\u7ec4\u5bf9\u8c61\u90fd\u53ef\u7528\u4e8e\u521b\u5efa\u8fed\u4ee3\u5668\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b(Python 3.0+)<\/h2>\n\n\n\n<p>&gt;&gt;&gt;&nbsp;list=[1,2,3,4]<br>&gt;&gt;&gt;&nbsp;it&nbsp;=&nbsp;iter(list)&nbsp;# \u521b\u5efa\u8fed\u4ee3\u5668\u5bf9\u8c61<br>&gt;&gt;&gt;&nbsp;<strong>print<\/strong>&nbsp;(next(it))&nbsp;&nbsp;# \u8f93\u51fa\u8fed\u4ee3\u5668\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20<br>1<br>&gt;&gt;&gt;&nbsp;<strong>print<\/strong>&nbsp;(next(it))<br>2<br>&gt;&gt;&gt;<\/p>\n\n\n\n<p>\u8fed\u4ee3\u5668\u5bf9\u8c61\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c4for\u8bed\u53e5\u8fdb\u884c\u904d\u5386\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b(Python 3.0+)<\/h2>\n\n\n\n<p>#!\/usr\/bin\/python3 list=[1,2,3,4] it = iter(list) # \u521b\u5efa\u8fed\u4ee3\u5668\u5bf9\u8c61 for x in it: print (x, end=&#8221; &#8220;)<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1 2 3 4<\/pre>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528 next() \u51fd\u6570\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b(Python 3.0+)<\/h2>\n\n\n\n<p>#!\/usr\/bin\/python3 import sys # \u5f15\u5165 sys \u6a21\u5757 list=[1,2,3,4] it = iter(list) # \u521b\u5efa\u8fed\u4ee3\u5668\u5bf9\u8c61 while True: try: print (next(it)) except StopIteration: sys.exit()<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\n2\n3\n4<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668<\/h3>\n\n\n\n<p>\u628a\u4e00\u4e2a\u7c7b\u4f5c\u4e3a\u4e00\u4e2a\u8fed\u4ee3\u5668\u4f7f\u7528\u9700\u8981\u5728\u7c7b\u4e2d\u5b9e\u73b0\u4e24\u4e2a\u65b9\u6cd5 __iter__() \u4e0e __next__() \u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u5df2\u7ecf\u4e86\u89e3\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\uff0c\u5c31\u77e5\u9053\u7c7b\u90fd\u6709\u4e00\u4e2a\u6784\u9020\u51fd\u6570\uff0cPython \u7684\u6784\u9020\u51fd\u6570\u4e3a __init__(), \u5b83\u4f1a\u5728\u5bf9\u8c61\u521d\u59cb\u5316\u7684\u65f6\u5019\u6267\u884c\u3002<\/p>\n\n\n\n<p>__iter__() \u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c \u8fd9\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u5b9e\u73b0\u4e86 __next__() \u65b9\u6cd5\u5e76\u901a\u8fc7 StopIteration \u5f02\u5e38\u6807\u8bc6\u8fed\u4ee3\u7684\u5b8c\u6210\u3002<\/p>\n\n\n\n<p>__next__() \u65b9\u6cd5\uff08Python 2 \u91cc\u662f next()\uff09\u4f1a\u8fd4\u56de\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u8fd4\u56de\u6570\u5b57\u7684\u8fed\u4ee3\u5668\uff0c\u521d\u59cb\u503c\u4e3a 1\uff0c\u9010\u6b65\u9012\u589e 1\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b(Python 3.0+)<\/h2>\n\n\n\n<p>class MyNumbers: def __iter__(self): self.a = 1 return self def __next__(self): x = self.a self.a += 1 return x myclass = MyNumbers() myiter = iter(myclass) print(next(myiter)) print(next(myiter)) print(next(myiter)) print(next(myiter)) print(next(myiter))<\/p>\n\n\n\n<p>\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\n2\n3\n4\n5<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">StopIteration<\/h3>\n\n\n\n<p>StopIteration \u5f02\u5e38\u7528\u4e8e\u6807\u8bc6\u8fed\u4ee3\u7684\u5b8c\u6210\uff0c\u9632\u6b62\u51fa\u73b0\u65e0\u9650\u5faa\u73af\u7684\u60c5\u51b5\uff0c\u5728 __next__() \u65b9\u6cd5\u4e2d\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u5728\u5b8c\u6210\u6307\u5b9a\u5faa\u73af\u6b21\u6570\u540e\u89e6\u53d1 StopIteration \u5f02\u5e38\u6765\u7ed3\u675f\u8fed\u4ee3\u3002<\/p>\n\n\n\n<p>\u5728 20 \u6b21\u8fed\u4ee3\u540e\u505c\u6b62\u6267\u884c\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b(Python 3.0+)<\/h2>\n\n\n\n<p>class MyNumbers: def __iter__(self): self.a = 1 return self def __next__(self): if self.a &lt;= 20: x = self.a self.a += 1 return x else: raise StopIteration myclass = MyNumbers() myiter = iter(myclass) for x in myiter: print(x)<\/p>\n\n\n\n<p>\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u751f\u6210\u5668<\/h2>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u4f7f\u7528\u4e86&nbsp;<strong>yield<\/strong>&nbsp;\u7684\u51fd\u6570\u88ab\u79f0\u4e3a\u751f\u6210\u5668\uff08generator\uff09\u3002<\/p>\n\n\n\n<p><strong>yield<\/strong>&nbsp;\u662f\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u5b9a\u4e49\u751f\u6210\u5668\u51fd\u6570\uff0c\u751f\u6210\u5668\u51fd\u6570\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u9010\u6b65\u4ea7\u751f\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u6240\u6709\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u8ddf\u666e\u901a\u51fd\u6570\u4e0d\u540c\u7684\u662f\uff0c\u751f\u6210\u5668\u662f\u4e00\u4e2a\u8fd4\u56de\u8fed\u4ee3\u5668\u7684\u51fd\u6570\uff0c\u53ea\u80fd\u7528\u4e8e\u8fed\u4ee3\u64cd\u4f5c\uff0c\u66f4\u7b80\u5355\u70b9\u7406\u89e3\u751f\u6210\u5668\u5c31\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002<\/p>\n\n\n\n<p>\u5f53\u5728\u751f\u6210\u5668\u51fd\u6570\u4e2d\u4f7f\u7528&nbsp;<strong>yield<\/strong>&nbsp;\u8bed\u53e5\u65f6\uff0c\u51fd\u6570\u7684\u6267\u884c\u5c06\u4f1a\u6682\u505c\uff0c\u5e76\u5c06&nbsp;<strong>yield<\/strong>&nbsp;\u540e\u9762\u7684\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5f53\u524d\u8fed\u4ee3\u7684\u503c\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u7136\u540e\uff0c\u6bcf\u6b21\u8c03\u7528\u751f\u6210\u5668\u7684&nbsp;<strong>next()<\/strong>&nbsp;\u65b9\u6cd5\u6216\u4f7f\u7528&nbsp;<strong>for<\/strong>&nbsp;\u5faa\u73af\u8fdb\u884c\u8fed\u4ee3\u65f6\uff0c\u51fd\u6570\u4f1a\u4ece\u4e0a\u6b21\u6682\u505c\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u518d\u6b21\u9047\u5230&nbsp;<strong>yield<\/strong>&nbsp;\u8bed\u53e5\u3002\u8fd9\u6837\uff0c\u751f\u6210\u5668\u51fd\u6570\u53ef\u4ee5\u9010\u6b65\u4ea7\u751f\u503c\uff0c\u800c\u4e0d\u9700\u8981\u4e00\u6b21\u6027\u8ba1\u7b97\u5e76\u8fd4\u56de\u6240\u6709\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>\u8c03\u7528\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u751f\u6210\u5668\u51fd\u6570\u7684\u4f7f\u7528\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>def countdown(n): while n &gt; 0: yield n n -= 1 # \u521b\u5efa\u751f\u6210\u5668\u5bf9\u8c61 generator = countdown(5) # \u901a\u8fc7\u8fed\u4ee3\u751f\u6210\u5668\u83b7\u53d6\u503c print(next(generator)) # \u8f93\u51fa: 5 print(next(generator)) # \u8f93\u51fa: 4 print(next(generator)) # \u8f93\u51fa: 3 # \u4f7f\u7528 for \u5faa\u73af\u8fed\u4ee3\u751f\u6210\u5668 for value in generator: print(value) # \u8f93\u51fa: 2 1<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u4e2d\uff0c<strong>countdown<\/strong>&nbsp;\u51fd\u6570\u662f\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u3002\u5b83\u4f7f\u7528 yield \u8bed\u53e5\u9010\u6b65\u4ea7\u751f\u4ece n \u5230 1 \u7684\u5012\u6570\u6570\u5b57\u3002\u5728\u6bcf\u6b21\u8c03\u7528 yield \u8bed\u53e5\u65f6\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\u5f53\u524d\u7684\u5012\u6570\u503c\uff0c\u5e76\u5728\u4e0b\u4e00\u6b21\u8c03\u7528\u65f6\u4ece\u4e0a\u6b21\u6682\u505c\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u521b\u5efa\u751f\u6210\u5668\u5bf9\u8c61\u5e76\u4f7f\u7528 next() \u51fd\u6570\u6216 for \u5faa\u73af\u8fed\u4ee3\u751f\u6210\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u9010\u6b65\u83b7\u53d6\u751f\u6210\u5668\u51fd\u6570\u4ea7\u751f\u7684\u503c\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 next() \u51fd\u6570\u83b7\u53d6\u524d\u4e09\u4e2a\u5012\u6570\u503c\uff0c\u7136\u540e\u901a\u8fc7 for \u5faa\u73af\u83b7\u53d6\u5269\u4e0b\u7684\u4e24\u4e2a\u5012\u6570\u503c\u3002<\/p>\n\n\n\n<p>\u751f\u6210\u5668\u51fd\u6570\u7684\u4f18\u52bf\u662f\u5b83\u4eec\u53ef\u4ee5\u6309\u9700\u751f\u6210\u503c\uff0c\u907f\u514d\u4e00\u6b21\u6027\u751f\u6210\u5927\u91cf\u6570\u636e\u5e76\u5360\u7528\u5927\u91cf\u5185\u5b58\u3002\u6b64\u5916\uff0c\u751f\u6210\u5668\u8fd8\u53ef\u4ee5\u4e0e\u5176\u4ed6\u8fed\u4ee3\u5de5\u5177\uff08\u5982for\u5faa\u73af\uff09\u65e0\u7f1d\u914d\u5408\u4f7f\u7528\uff0c\u63d0\u4f9b\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u8fed\u4ee3\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">5\n4\n3\n2\n1<\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4f7f\u7528 yield \u5b9e\u73b0\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b(Python 3.0+)<\/h2>\n\n\n\n<p>#!\/usr\/bin\/python3 import sys def fibonacci(n): # \u751f\u6210\u5668\u51fd\u6570 &#8211; \u6590\u6ce2\u90a3\u5951 a, b, counter = 0, 1, 0 while True: if (counter &gt; n): return yield a a, b = b, a + b counter += 1 f = fibonacci(10) # f \u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u7531\u751f\u6210\u5668\u8fd4\u56de\u751f\u6210 while True: try: print (next(f), end=&#8221; &#8220;) except StopIteration: sys.exit()<\/p>\n\n\n\n<p>\u6267\u884c\u4ee5\u4e0a\u7a0b\u5e8f\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0 1 1 2 3 5 8 13 21 34 55<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fed\u4ee3\u5668 \u8fed\u4ee3\u662f Python \u6700\u5f3a\u5927\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u662f\u8bbf\u95ee\u96c6\u5408\u5143\u7d20\u7684\u4e00\u79cd&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":488,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-568","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"views":463,"_links":{"self":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/comments?post=568"}],"version-history":[{"count":1,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"predecessor-version":[{"id":569,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/posts\/568\/revisions\/569"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media\/488"}],"wp:attachment":[{"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pyteam.cn\/index.php\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}