{"id":1395,"date":"2019-07-15T16:22:31","date_gmt":"2019-07-15T20:22:31","guid":{"rendered":"http:\/\/www.a2life.info\/w\/?p=1395"},"modified":"2019-07-15T16:22:31","modified_gmt":"2019-07-15T20:22:31","slug":"python-generator","status":"publish","type":"post","link":"https:\/\/www.a2life.info\/w\/?p=1395","title":{"rendered":"Python Generator"},"content":{"rendered":"<pre>&gt;&gt;&gt; def make_counter(x):\r\n\tprint('entering make_counter')\r\n\twhile True:\r\n\t\tyield x\r\n\t\tprint('incrementing x')\r\n\t\tx = x + 1\r\n\r\n\t\t\r\n&gt;&gt;&gt; counter = make_counter(2)\r\n&gt;&gt;&gt; counter\r\n\r\n&gt;&gt;&gt; next(counter)\r\nentering make_counter\r\n2\r\n&gt;&gt;&gt; next(counter)\r\nincrementing x\r\n3\r\n&gt;&gt;&gt; next(counter)\r\nincrementing x\r\n4\r\n&gt;&gt;&gt; next(counter)\r\nincrementing x\r\n5\r\n&gt;&gt;&gt; def fib(max):\r\n\ta,b = 0,1\r\n\twhile a &lt; max: yield a a,b = b, a + b &gt;&gt;&gt; for n in fib(1000):\r\n\tprint(n,end = ' ')\r\n\r\n\t\r\n0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 \r\n&gt;&gt;&gt; list(fib(1000))\r\n[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]\r\n&gt;&gt;&gt; \r\n<\/pre>\n<p>Python Generator \u306e\u8a71<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&gt;&gt;&gt; def make_counter(x): print(&#8216;entering make_counter&#8217;) while True: yield x print(&#8216;incrementing x&#8217;) x = x + 1 &gt;&gt;&gt; counter = make_counter(2) &gt;&gt;&gt; counter &gt;&gt;&gt; next(counter) entering make_counter 2 &gt;&gt;&gt; next(counter) incrementing x 3 &gt;&gt;&gt; next(counter) incrementing x 4 &gt;&gt;&gt; next(counter) incrementing x 5 &gt;&gt;&gt; def fib(max): a,b = 0,1 while a &lt; max: yield a a,b = b, a + b &gt;&gt;&gt; for n in fib(1000): print(n,end = &#8216; &#8216;) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 &gt;&gt;&gt; list(fib(1000)) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] &gt;&gt;&gt; Python Generator \u306e\u8a71<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-1395","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1395"}],"version-history":[{"count":4,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1395\/revisions"}],"predecessor-version":[{"id":1772,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=\/wp\/v2\/posts\/1395\/revisions\/1772"}],"wp:attachment":[{"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.a2life.info\/w\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}