{"id":89,"date":"2016-04-06T21:30:16","date_gmt":"2016-04-06T13:30:16","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=89"},"modified":"2016-04-06T22:43:14","modified_gmt":"2016-04-06T14:43:14","slug":"%e7%ac%ac7%e7%ab%a0-%e7%b1%bb%e5%92%8citerators","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=89","title":{"rendered":"\u7b2c7\u7ae0 \u7c7b\u548cIterators"},"content":{"rendered":"<p><strong><span style=\"color: #000000;\">\uff081\uff09\u5b9a\u4e49\u7c7b\uff0c\u53ea\u8981\u4ee5\u4fdd\u7559\u5b57class\u5f00\u5934\uff0c\u7136\u540e\u8ddf\u968f\u201c\u7c7b\u540d\u201d\u5c31\u53ef\u4ee5\uff1a<\/span><\/strong><\/p>\n<blockquote><p><code>class PapayaWhip:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2460<\/code><br \/>\n<code>\u00a0\u00a0\u00a0 pass\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/code>\u2461<\/p><\/blockquote>\n<ol>\n<li>\u7c7b\u540dPapayaWhip\uff0c\u5e76\u4e14\u5b83\u6ca1\u6709\u7ee7\u627f\u4efb\u4f55\u5176\u5b83\u7c7b\u3002\u901a\u5e38\u7c7b\u540d\u5927\u5199\u5355\u8bcd\u9996\u5b57\u6bcd\uff0c\u8fd9\u662f\u4e2a\u4e60\u4fd7\u4e0d\u662f\u8bed\u6cd5\u8981\u6c42\u3002<\/li>\n<li>\u7c7b\u4e2d\u7684\u7684\u8bed\u53e5\u90fd\u8981\u7f29\u8fdb\uff0c\u7b2c\u4e00\u53e5\u6ca1\u6709\u7f29\u8fdb\u7684\u8bed\u53e5\uff0c\u4ee3\u8868\u7c7b\u7684\u7ed3\u675f\u3002pass\u662f\u4e00\u4e2a\u5360\u4f4d\u5173\u952e\u5b57\uff0c\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><strong>\uff082\uff09C++\u7a0b\u5e8f\u5458\u53ef\u80fd\u4f1a\u611f\u89c9\u5947\u602a\uff0c\u8fd9\u4e2a\u7c7b\u4e2d\u6ca1\u6709\u6784\u9020\u51fd\u6570\u4e5f\u6ca1\u6709\u6790\u6784\u51fd\u6570\uff0c\u4f46\u662f\u5728python\u4e2d\u6709\u53e6\u4e00\u4e2a\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff1a__init__()<\/strong><\/p>\n<blockquote><p><code>class Fib:<\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 '''iterator that yields numbers in the Fibonacci sequence'''\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u2460<\/code><br \/>\n<code>\u00a0\u00a0\u00a0\u00a0 def __init__(self, max):\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u2461<\/code><\/p><\/blockquote>\n<ol>\n<li>\u7c7b\u548c\u6a21\u5757\u3001\u51fd\u6570\u4e00\u6837\u53ef\u4ee5\u6709docstring<\/li>\n<li>__init__()\u65b9\u6cd5\u5728\u5b9e\u4f8b\u5efa\u7acb\u540e\u4f1a\u88ab\u7acb\u5373\u8c03\u7528\u3002\u6280\u672f\u4e0a\u8bb2\u4e0d\u5e94\u8be5\u624b\u52a8\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u3002<\/li>\n<li>\u6bcf\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u4e2d\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u5305\u542b__init__()\u65b9\u6cd5\uff09\uff0c\u4e00\u76f4\u662f\u7c7b\u5b9e\u4f8b\u7684\u5f15\u7528\u2014\u2014\u6839\u636e\u4e60\u4fd7\u88ab\u547d\u540d\u4e3aself\u3002\u8fd9\u4e2a\u53c2\u6570\u5360\u636e\u4e86c++\u548cjava\u7684this\u4fdd\u7559\u5b57\u7684\u4f4d\u7f6e\uff0c\u4f46\u662fself\u5728python\u4e2d\u5e76\u4e0d\u662f\u4e00\u4e2a\u4fdd\u7559\u5b57\uff0c\u4ec5\u4ec5\u662f\u4e00\u4e2a\u547d\u540d\u4e60\u60ef\u3002\uff08\u6ce8\uff1a\u975e\u5e38\u5f3a\u786c\u7684\u4e60\u4fd7\uff0c\u4e0d\u8981\u7528\u522b\u7684\uff0c\u53ea\u7528self\uff0c\u8bb0\u4f4f\uff09<\/li>\n<li>\u5728__init__()\u65b9\u6cd5\u4e2dself\u6307\u5411\u4e00\u4e2a\u65b0\u5efa\u5bf9\u8c61\uff1b\u5728\u7c7b\u7684\u5176\u5b83\u65b9\u6cd5\u4e2d\u5b83\u6307\u5411\u8c03\u7528\u65b9\u6cd5\u7684\u5b9e\u4f8b\u3002\u867d\u7136\u5728\u5b9a\u4e49\u65b9\u6cd5\u65f6\u8981\u663e\u793a\u7684\u5199self\uff0c\u4f46\u5728\u8c03\u7528\u65b9\u6cd5\u7684\u65f6\u5019\u4e0d\u7528\u663e\u793a\u7684\u5199self\u3002<\/li>\n<\/ol>\n<p><strong>\uff083\uff09\u5b9e\u4f8b\u5316\u4e00\u4e2a\u7c7b\uff0c\u5c31\u50cf\u4f7f\u7528\u51fd\u6570\u4e00\u6837\uff0c\u5199\u4e0a__init__()\u6240\u9700\u7684\u53c2\u6570\u5c31\u53ef\u4ee5\uff1a<\/strong><\/p>\n<blockquote><p><code>&gt;&gt;&gt; import fibonacci2<\/code><br \/>\n<code> &gt;&gt;&gt; fib = fibonacci2.Fib(100)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0 \u00a0 \u2460<\/code><br \/>\n<code> &gt;&gt;&gt; fib \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 \u00a0 \u00a0 \u2461<\/code><br \/>\n<code> &lt;fibonacci2.Fib object at 0x00DB8810&gt;<\/code><br \/>\n<code> &gt;&gt;&gt; fib.__class__\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 \u00a0 \u2462<\/code><br \/>\n<code> &lt;class 'fibonacci2.Fib'&gt;<\/code><br \/>\n<code> &gt;&gt;&gt; fib.__doc__\u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2463<\/code><br \/>\n<code> 'iterator that yields numbers in the Fibonacci sequence'<\/code><\/p><\/blockquote>\n<ol>\n<li>\u521b\u5efa\u4e86\u4e00Fib\u7c7b\u7684\u5b9e\u4f8b\uff08fibonacci2\u6a21\u5757\u5305\uff09\uff0c\u65b0\u5efa\u7684\u5b9e\u4f8b\u5206\u914d\u5230\u53d8\u91cffib\u4e0a\u3002\u6240\u4f20\u7684\u53c2\u6570\u6b63\u662f__init__()\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/li>\n<li>\u73b0\u5728fib\u662fFib\u7c7b\u7684\u4e00\u4e2a\u5b9e\u4f8b\u4e86\u3002<\/li>\n<li>\u6bcf\u4e2a\u7c7b\u5b9e\u4f8b\u90fd\u6709\u4e00\u4e2a\u5185\u5efa\u5c5e\u6027__class__\u2014\u2014\u5b83\u6307\u660e\u5bf9\u8c61\u7684\u7c7b\u3002<\/li>\n<li>\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684docstring\uff0c\u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u76f8\u540c\u7684docstring.<\/li>\n<\/ol>\n<p><strong>\uff084\uff09\u5b9e\u4f8b\u53d8\u91cf<\/strong><\/p>\n<p>\u5b9a\u4e49\u5728\u7c7b\u5185\u90e8\u7684\u53d8\u91cf\uff0c\u5f52\u5c5e\u4e8e\u7c7b<\/p>\n<p><strong>\uff085\uff09Iterator\u2014\u2014\u8fed\u4ee3\u5668<\/strong><\/p>\n<ol>\n<li>\u8fed\u4ee3\u5668\u5c31\u662f\u7c7b\u4e2d\u5b9a\u4e49\u7684__iter__()\u65b9\u6cd5\u3002<\/li>\n<li>\u5728\u7c7b\u4e0a\u8c03\u7528next()\u65f6\uff0c\u7c7b\u4e2d\u7684__next__()\u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\u3002\u4e0d\u8981\u5728__next__()\u65b9\u6cd5\u91cc\u4f7f\u7528yield\u8fd4\u56de\u503c\uff0c\u7528return\u4ee3\u66ff\u3002<\/li>\n<li>\u4ee5for n in Fib(1000)\u4e3a\u4f8b\u89e3\u91ca\u5176\u4e2d\u53d1\u751f\u4e86\u4ec0\u4e48\uff1a<br \/>\n<strong>\u9996\u5148<\/strong>\uff0cFib(1000)\u5efa\u7acb\u4e86\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5047\u8bbe\u540d\u4e3afib_inst\uff1b<br \/>\n<strong>\u5176\u6b21<\/strong>\uff0cfor\u5faa\u73af\u4f1a\u8c03\u7528iter(fib_inst)\uff0c\u5b83\u4f1a\u6267\u884cfib__inst.__iter__()\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u2014\u2014\u5047\u8bbe\u540d\u4e3afib_iter\u3002<br \/>\n<strong>\u7136\u540e<\/strong>\uff0cfor\u5faa\u73af\u4f1a\u8c03\u7528next(fib_iter)\u2014\u2014fib_iter.__next__()\uff0cfor\u5faa\u73af\u4f1a\u5f97\u5230\u8fd9\u4e2a\u8c03\u7528\u7684\u8fd4\u56de\u503c\uff0c\u5e76\u628a\u5b83\u8d4b\u7ed9n\uff0c\u7136\u540e\u518d\u8fdb\u5165\u5faa\u73af\u4f53\u3002<br \/>\n<strong>\u6700\u540e<\/strong>\uff0c\u5f53__next__()\u5f15\u53d1\u4e00\u4e2aStopIteration\u5f02\u5e38\u65f6\uff0c\u5faa\u73af\u505c\u6b62\u3002<\/li>\n<\/ol>\n<p><code>def __iter__(self):<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 self.a=0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 self.b=1<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 return self<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>def __next__(self):<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 fib=self.a<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 if ifb &gt; self.max:<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 raise StopIteration<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 self.a, self.b = self.b, self.a+self.b<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0 return fib<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uff081\uff09\u5b9a\u4e49\u7c7b\uff0c\u53ea\u8981\u4ee5\u4fdd\u7559\u5b57class\u5f00\u5934\uff0c\u7136\u540e\u8ddf\u968f\u201c\u7c7b\u540d\u201d\u5c31\u53ef\u4ee5\uff1a class PapayaWhip:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u2460 \u00a0\u00a0\u00a0 pass\u00a0\u00a0\u00a0\u00a0&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=89\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","hentry","category-dive-into-python3"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":8,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/109"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}