{"id":370,"date":"2022-02-08T16:55:43","date_gmt":"2022-02-08T08:55:43","guid":{"rendered":"http:\/\/47.98.165.52\/?p=370"},"modified":"2022-02-08T16:55:43","modified_gmt":"2022-02-08T08:55:43","slug":"%e8%bf%ad%e4%bb%a3%e5%99%a8","status":"publish","type":"post","link":"\/?p=370","title":{"rendered":"\u8fed\u4ee3\u5668"},"content":{"rendered":"<p><strong>\u6982\u5ff5\uff1a<\/strong>\u5b9e\u73b0\u8fed\u4ee3\u5668\u534f\u8bae\u7684\u5bb9\u5668\u5bf9\u8c61\u3002\u5b83\u57fa\u4e8e\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><strong>next<\/strong> :\u8fd4\u56de\u5bb9\u5668\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20<\/li>\n<li><strong>iter<\/strong> :\u8fd4\u56de\u8fed\u4ee3\u5668\u672c\u8eab<\/li>\n<\/ul>\n<p>\u521b\u5efa\u65b9\u6cd5\uff1a<\/p>\n<p>1.\u5229\u7528\u5185\u7f6e\u51fd\u6570iter()\u521b\u5efa<br \/>\niter()\u51fd\u6570\u5305\u88f9\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\u5e8f\u5217\u5373\u53ef\u6210\u4e3a\u8fed\u4ee3\u5668\u3002<\/p>\n<pre><code class=\"language-python\">In [52]: i = iter(&#039;zxcvbnm&#039;)\n\nIn [53]: i.__next__\nOut[53]: &lt;method-wrapper &#039;__next__&#039; of str_iterator object at 0x000002BB70F9FE48&gt;\n\nIn [54]: i.__next__()\nOut[54]: &#039;z&#039;\n\nIn [55]: i.__next__()\nOut[55]: &#039;x&#039;\n\nIn [56]: next(i)\nOut[56]: &#039;c&#039;<\/code><\/pre>\n<p>2.\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u8fed\u4ee3\u5668<br \/>\n\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5177\u6709<code> __next__<\/code>\u65b9\u6cd5\u7684\u7c7b\uff0c\u53ea\u8981\u8fd9\u4e2a\u7c7b\u63d0\u4f9b\u8fd4\u56de\u8fed\u4ee3\u5668\u793a\u4f8b\u7684<code>__iter__<\/code>\u7279\u6b8a\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"language-python\">class CountDown:\n    def __init__(self, step):\n        self.step = step\n    def __next__(self):\n        &quot;&quot;&quot;return the next element&quot;&quot;&quot;\n        if self.step &lt;= 0:\n            raise StopIteration\n        self.step -= 1\n        return self.step\n    def __iter__(self):\n        &quot;&quot;&quot;return the iterator itself&quot;&quot;&quot;\n        return self<\/code><\/pre>\n<p>\u7528\u6cd5\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">In [63]: for element in CountDown(4):\n    ...:     print(element)\n    ...:     \n3\n2\n1\n0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u5ff5\uff1a\u5b9e\u73b0\u8fed\u4ee3\u5668\u534f\u8bae\u7684\u5bb9\u5668\u5bf9\u8c61\u3002\u5b83\u57fa\u4e8e\u4ee5\u4e0b\u4e24\u4e2a\u65b9\u6cd5\uff1a next :\u8fd4\u56de\u5bb9\u5668\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20 iter :\u8fd4\u56de\u8fed\u4ee3\u5668\u672c\u8eab \u521b\u5efa\u65b9\u6cd5\uff1a 1.\u5229\u7528   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[34],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/370"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=370"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":371,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/370\/revisions\/371"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}