{"id":2870,"date":"2022-03-11T16:18:05","date_gmt":"2022-03-11T08:18:05","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2870"},"modified":"2022-03-11T16:18:06","modified_gmt":"2022-03-11T08:18:06","slug":"python3-yeild","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/03\/11\/python3-yeild\/","title":{"rendered":"Python3 \u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668"},"content":{"rendered":"\n<h2>\u8fed\u4ee3\u5668<\/h2>\n\n\n\n<p>\u8fed\u4ee3\u662fPython\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 class=\"has-light-pink-background-color has-background\">\u8fed\u4ee3\u5668\u6709\u4e24\u4e2a\u57fa\u672c\u7684\u65b9\u6cd5\uff1a<strong>iter()<\/strong>\u00a0\u7528\u4e8e\u751f\u6210\u8fed\u4ee3\u5668 \u548c\u00a0<strong>next() \u6c42\u8fed\u4ee3\u5668\u7684\u503c<\/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<pre class=\"wp-block-code\"><code>>>> list=&#91;1,2,3,4]\r\n>>> it = iter(list)    # \u521b\u5efa\u8fed\u4ee3\u5668\u5bf9\u8c61\r\n>>> print (next(it))   # \u8f93\u51fa\u8fed\u4ee3\u5668\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\r\n1\r\n>>> print (next(it))\r\n2\r\n>>><\/code><\/pre>\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<pre class=\"wp-block-code\"><code>list=&#91;1,2,3,4]\r\nit = iter(list)    # \u521b\u5efa\u8fed\u4ee3\u5668\u5bf9\u8c61\r\nfor x in it:\r\n    print (x, end=\" \")<\/code><\/pre>\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<pre class=\"wp-block-code\"><code>import sys         # \u5f15\u5165 sys \u6a21\u5757\r\n \r\nlist=&#91;1,2,3,4]\r\nit = iter(list)    # \u521b\u5efa\u8fed\u4ee3\u5668\u5bf9\u8c61\r\n \r\nwhile True:\r\n    try:\r\n        print (next(it))\r\n    except StopIteration:\r\n        sys.exit()<\/code><\/pre>\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>\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>\u66f4\u591a\u5185\u5bb9\u67e5\u9605\uff1a<a href=\"https:\/\/www.runoob.com\/python3\/python3-class.html\" rel=\"noreferrer noopener\" target=\"_blank\">Python3 \u9762\u5411\u5bf9\u8c61<\/a><\/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<pre class=\"wp-block-code\"><code>class MyNumbers:\r\n  def __iter__(self):\r\n    self.a = 1\r\n    return self\r\n \r\n  def __next__(self):\r\n    x = self.a\r\n    self.a += 1\r\n    return x\r\n \r\nmyclass = MyNumbers()\r\nmyiter = iter(myclass)\r\n \r\nprint(next(myiter))\r\nprint(next(myiter))\r\nprint(next(myiter))\r\nprint(next(myiter))\r\nprint(next(myiter))<\/code><\/pre>\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>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<pre class=\"wp-block-code\"><code>class MyNumbers:\r\n  def __iter__(self):\r\n    self.a = 1\r\n    return self\r\n \r\n  def __next__(self):\r\n    if self.a &lt;= 20:\r\n      x = self.a\r\n      self.a += 1\r\n      return x\r\n    else:\r\n      raise StopIteration\r\n \r\nmyclass = MyNumbers()\r\nmyiter = iter(myclass)\r\n \r\nfor x in myiter:\r\n  print(x)<\/code><\/pre>\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<h2>\u751f\u6210\u5668<\/h2>\n\n\n\n<p>yield\u662fpython\u7684\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u751f\u6210\u5668generator\u3002<\/p>\n\n\n\n<p class=\"has-light-pink-background-color has-background\">\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u5b9a\u4e49\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u540c\u5b9a\u4e49\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u7279\u6b8a\u4e4b\u5904\u5728\u4e8e\u751f\u6210\u5668\u51fd\u6570\u5185\u90e8\u4f1a\u5305\u542byield\u8868\u8fbe\u5f0f\uff0c\u4e13\u95e8\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5e8f\u5217\u3002\u5f53\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u4e4b\u540e\u5c31\u7531\u8fd9\u4e2a\u8fed\u4ee3\u5668\u6765\u63a7\u5236\u751f\u6210\u5668\u51fd\u6570\u7684\u6267\u884c\u3002\u5f53\u751f\u6210\u5668\u51fd\u6570\u88ab\u8c03\u7528\u540e\uff0c\u9996\u5148\u4f1a\u6267\u884c\u5230\u7b2c\u4e00\u4e2ayield\u8868\u8fbe\u5f0f\u5904\uff0c\u7136\u540e\u4f1a\u5c06\u751f\u6210\u5668\u51fd\u6570\u6302\u8d77\uff0c\u5c06yield\u751f\u6210\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u8fd4\u56de\u7ed9\u751f\u6210\u5668\u51fd\u6570\u7684\u8c03\u7528\u8005\u3002\u5f53\u751f\u6210\u5668\u51fd\u6570\u88ab\u6302\u8d77\u65f6\uff0c\u5b83\u7684\u6240\u6709\u5c40\u90e8\u72b6\u6001\u90fd\u4f1a\u88ab\u4fdd\u5b58\u8d77\u6765\uff0c\u5305\u62ec\u5f53\u524d\u7ed1\u5b9a\u7684\u5c40\u90e8\u53d8\u91cf\u3001\u6307\u4ee4\u6307\u9488\u3001\u5185\u90e8\u6808\u548c\u5f02\u5e38\u5904\u7406\u7684\u72b6\u6001\u3002\u5f53\u8c03\u7528\u8fed\u4ee3\u5668\u7684\u65b9\u6cd5\u65f6\uff0c\u4e00\u822c\u90fd\u662f\u8c03\u7528next()\u65b9\u6cd5\uff0c\u5c06\u4f1a\u6062\u590d\u751f\u6210\u5668\u51fd\u6570\u7684\u6267\u884c\uff0c\u5e76\u4e14\u662f\u4ece\u4e0a\u6b21\u88ab\u6302\u8d77\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u9047\u5230\u53e6\u5916\u4e00\u6b21yield\u8c03\u7528\uff0c\u751f\u6210\u5668\u51fd\u6570\u5c06\u518d\u6b21\u88ab\u6302\u8d77\u3002<\/p>\n\n\n\n<p>\u5728\u4e00\u4e2a\u751f\u6210\u5668\u51fd\u6570\u4e2d\uff0c\u5982\u679c\u6ca1\u6709 return\uff0c\u5219\u9ed8\u8ba4\u6267\u884c\u81f3\u51fd\u6570\u5b8c\u6bd5\uff0c\u5982\u679c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d return\uff0c\u5219\u76f4\u63a5\u629b\u51fa StopIteration \u7ec8\u6b62\u8fed\u4ee3\u3002<\/p>\n\n\n\n<p><strong>\u53ef\u4ee5\u4e2d\u65ad\u4e00\u4e2a\u51fd\u6570\u7684\u6267\u884c\uff0c\u8df3\u8f6c\u5230\u8c03\u7528\u7684\u5730\u65b9<\/strong><\/p>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u4f7f\u7528\u4e86 yield \u7684\u51fd\u6570\u88ab\u79f0\u4e3a\u751f\u6210\u5668\uff08generator\uff09\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 class=\"has-light-pink-background-color has-background\">\u5728\u8c03\u7528\u751f\u6210\u5668\u8fd0\u884c\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u6b21\u9047\u5230 yield \u65f6\u51fd\u6570\u4f1a\u6682\u505c\u5e76\u4fdd\u5b58\u5f53\u524d\u6240\u6709\u7684\u8fd0\u884c\u4fe1\u606f\uff0c\u8fd4\u56de yield \u7684\u503c, \u5e76\u5728\u4e0b\u4e00\u6b21\u6267\u884c next() \u65b9\u6cd5\u65f6\u4ece\u5f53\u524d\u4f4d\u7f6e\u7ee7\u7eed\u8fd0\u884c\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>\u4ee5\u4e0b\u5b9e\u4f8b\u4f7f\u7528 yield \u5b9e\u73b0\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\r\n \r\ndef fibonacci(n): # \u751f\u6210\u5668\u51fd\u6570 - \u6590\u6ce2\u90a3\u5951\r\n    a, b, counter = 0, 1, 0\r\n    while True:\r\n        if (counter > n): \r\n            return\r\n        yield a\r\n        a, b = b, a + b\r\n        counter += 1\r\nf = fibonacci(10) # f \u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u7531\u751f\u6210\u5668\u8fd4\u56de\u751f\u6210\r\n \r\nwhile True:\r\n    try:\r\n        print (next(f), end=\" \")\r\n    except StopIteration:\r\n        sys.exit()<\/code><\/pre>\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","protected":false},"excerpt":{"rendered":"<p>\u8fed\u4ee3\u5668 \u8fed\u4ee3\u662fPython\u6700\u5f3a\u5927\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u662f\u8bbf\u95ee\u96c6\u5408\u5143\u7d20\u7684\u4e00\u79cd\u65b9\u5f0f\u3002 \u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u53ef\u4ee5\u8bb0\u4f4f\u904d\u5386\u7684\u4f4d\u7f6e\u7684\u5bf9\u8c61\u3002 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/03\/11\/python3-yeild\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Python3 \u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2870"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2870"}],"version-history":[{"count":11,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2870\/revisions"}],"predecessor-version":[{"id":2881,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2870\/revisions\/2881"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2870"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}