{"id":511,"date":"2019-08-17T09:57:21","date_gmt":"2019-08-17T01:57:21","guid":{"rendered":"https:\/\/www.linuxdevops.cn\/?p=511"},"modified":"2019-10-15T16:17:29","modified_gmt":"2019-10-15T08:17:29","slug":"3scqg","status":"publish","type":"post","link":"https:\/\/www.linuxdevops.cn\/2019\/08\/3scqg\/","title":{"rendered":"3.0 \u751f\u6210\u5668 Generators"},"content":{"rendered":"\n
\u9996\u5148\u6211\u4eec\u8981\u7406\u89e3\u8fed\u4ee3\u5668(iterators)\u3002\u6839\u636e\u7ef4\u57fa\u767e\u79d1\uff0c\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u8ba9\u7a0b\u5e8f\u5458\u53ef\u4ee5\u904d\u5386\u4e00\u4e2a\u5bb9\u5668\uff08\u7279\u522b\u662f\u5217\u8868\uff09\u7684\u5bf9\u8c61\u3002\u7136\u800c\uff0c\u4e00\u4e2a\u8fed\u4ee3\u5668\u5728\u904d\u5386\u5e76\u8bfb\u53d6\u4e00\u4e2a\u5bb9\u5668\u7684\u6570\u636e\u5143\u7d20\u65f6\uff0c\u5e76\u4e0d\u4f1a\u6267\u884c\u4e00\u4e2a\u8fed\u4ee3\u3002\u4f60\u53ef\u80fd\u6709\u70b9\u6655\u4e86\uff0c\u90a3\u6211\u4eec\u6765\u4e2a\u6162\u52a8\u4f5c\u3002\u6362\u53e5\u8bdd\u8bf4\u8fd9\u91cc\u6709\u4e09\u4e2a\u90e8\u5206\uff1a<\/p>\n\n\n\n
\u4e0a\u9762\u8fd9\u4e9b\u90e8\u5206\u4e92\u76f8\u8054\u7cfb\u3002\u6211\u4eec\u4f1a\u5148\u5404\u4e2a\u51fb\u7834\u6765\u8ba8\u8bba\u4ed6\u4eec\uff0c\u7136\u540e\u518d\u8ba8\u8bba\u751f\u6210\u5668(generators).<\/p>\n\n\n\n
Python\u4e2d\u4efb\u610f\u7684\u5bf9\u8c61\uff0c\u53ea\u8981\u5b83\u5b9a\u4e49\u4e86\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u7684 \u4efb\u610f\u5bf9\u8c61\uff0c\u53ea\u8981\u5b9a\u4e49\u4e86 \u7528\u7b80\u5355\u7684\u8bdd\u8bb2\uff0c\u5b83\u5c31\u662f\u4ece\u67d0\u4e2a\u5730\u65b9\uff08\u6bd4\u5982\u4e00\u4e2a\u5217\u8868\uff09\u53d6\u51fa\u4e00\u4e2a\u5143\u7d20\u7684\u8fc7\u7a0b\u3002\u5f53\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u904d\u5386\u67d0\u4e2a\u4e1c\u897f\u65f6\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u672c\u8eab\u5c31\u53eb\u8fed\u4ee3\u3002\u73b0\u5728\u65e2\u7136\u6211\u4eec\u6709\u4e86\u8fd9\u4e9b\u672f\u8bed\u7684\u57fa\u672c\u7406\u89e3\uff0c\u90a3\u6211\u4eec\u5f00\u59cb\u7406\u89e3\u751f\u6210\u5668\u5427<\/p>\n\n\n\n \u751f\u6210\u5668\u4e5f\u662f\u4e00\u79cd\u8fed\u4ee3\u5668\uff0c\u4f46\u662f\u4f60\u53ea\u80fd\u5bf9\u5176\u8fed\u4ee3\u4e00\u6b21\u3002\u8fd9\u662f\u56e0\u4e3a\u5b83\u4eec\u5e76\u6ca1\u6709\u628a\u6240\u6709\u7684\u503c\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u800c\u662f\u5728\u8fd0\u884c\u65f6\u751f\u6210\u503c\u3002\u4f60\u901a\u8fc7\u904d\u5386\u6765\u4f7f\u7528\u5b83\u4eec\uff0c\u8981\u4e48\u7528\u4e00\u4e2a\u201cfor\u201d\u5faa\u73af\uff0c\u8981\u4e48\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9\u4efb\u610f\u53ef\u4ee5\u8fdb\u884c\u8fed\u4ee3\u7684\u51fd\u6570\u548c\u7ed3\u6784\u3002\u5927\u591a\u6570\u65f6\u5019\u751f\u6210\u5668\u662f\u4ee5\u51fd\u6570\u6765\u5b9e\u73b0\u7684\u3002\u7136\u800c\uff0c\u5b83\u4eec\u5e76\u4e0d\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u800c\u662f \u8fd9\u4e2a\u6848\u4f8b\u5e76\u4e0d\u662f\u975e\u5e38\u5b9e\u7528\u3002\u751f\u6210\u5668\u6700\u4f73\u5e94\u7528\u573a\u666f\u662f\uff1a\u4f60\u4e0d\u60f3\u540c\u4e00\u65f6\u95f4\u5c06\u6240\u6709\u8ba1\u7b97\u51fa\u6765\u7684\u5927\u91cf\u7ed3\u679c\u96c6\u5206\u914d\u5230\u5185\u5b58\u5f53\u4e2d\uff0c\u7279\u522b\u662f\u7ed3\u679c\u96c6\u91cc\u8fd8\u5305\u542b\u5faa\u73af\u3002<\/p>\n\n\n\n \u8bd1\u8005\u6ce8\uff1a\u8fd9\u6837\u505a\u4f1a\u6d88\u8017\u5927\u91cf\u8d44\u6e90<\/p><\/blockquote>\n\n\n\n \u8bb8\u591aPython 2\u91cc\u7684\u6807\u51c6\u5e93\u51fd\u6570\u90fd\u4f1a\u8fd4\u56de\u5217\u8868\uff0c\u800cPython 3\u90fd\u4fee\u6539\u6210\u4e86\u8fd4\u56de\u751f\u6210\u5668\uff0c\u56e0\u4e3a\u751f\u6210\u5668\u5360\u7528\u66f4\u5c11\u7684\u8d44\u6e90\u3002<\/p>\n\n\n\n \u4e0b\u9762\u662f\u4e00\u4e2a\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u7684\u751f\u6210\u5668\uff1a<\/p>\n\n\n\n \u51fd\u6570\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n \u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u7528\u62c5\u5fc3\u5b83\u4f1a\u4f7f\u7528\u5927\u91cf\u8d44\u6e90\u3002\u7136\u800c\uff0c\u4e4b\u524d\u5982\u679c\u6211\u4eec\u8fd9\u6837\u6765\u5b9e\u73b0\u7684\u8bdd\uff1a<\/p>\n\n\n\n \u8fd9\u4e5f\u8bb8\u4f1a\u5728\u8ba1\u7b97\u5f88\u5927\u7684\u8f93\u5165\u53c2\u6570\u65f6\uff0c\u7528\u5c3d\u6240\u6709\u7684\u8d44\u6e90\u3002\u6211\u4eec\u5df2\u7ecf\u8ba8\u8bba\u8fc7\u751f\u6210\u5668\u4f7f\u7528\u4e00\u6b21\u8fed\u4ee3\uff0c\u4f46\u6211\u4eec\u5e76\u6ca1\u6709\u6d4b\u8bd5\u8fc7\u3002\u5728\u6d4b\u8bd5\u524d\u4f60\u9700\u8981\u518d\u77e5\u9053\u4e00\u4e2aPython\u5185\u7f6e\u51fd\u6570\uff1a \u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5728 \u597d\u5427\uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u9884\u671f\u7684\u3002\u8fd9\u4e2a\u5f02\u5e38\u8bf4\u90a3\u4e2a \u73b0\u5728\u597d\u591a\u5566\u3002\u6211\u80af\u5b9a\u4f60\u5df2\u7ecf\u7231\u4e0a\u4e86\u5b66\u4e60\u751f\u6210\u5668\u3002\u4e00\u5b9a\u8981\u8bb0\u4f4f\uff0c\u60f3\u8981\u5b8c\u5168\u638c\u63e1\u8fd9\u4e2a\u6982\u5ff5\uff0c\u4f60\u53ea\u6709\u4f7f\u7528\u5b83\u3002\u786e\u4fdd\u4f60\u6309\u7167\u8fd9\u4e2a\u6a21\u5f0f\uff0c\u5e76\u5728\u751f\u6210\u5668\u5bf9\u4f60\u6709\u610f\u4e49\u7684\u4efb\u4f55\u65f6\u5019\u90fd\u4f7f\u7528\u5b83\u3002\u4f60\u7edd\u5bf9\u4e0d\u4f1a\u5931\u671b\u7684\uff01<\/p>\n\n\n\n \u751f\u6210\u5668\uff08Generators\uff09 \u9996\u5148\u6211\u4eec\u8981\u7406\u89e3\u8fed\u4ee3\u5668(iterators)\u3002\u6839\u636e\u7ef4\u57fa\u767e\u79d1\uff0c\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u8ba9\u7a0b\u5e8f\u5458<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[],"yoast_head":"\n__iter__<\/code>\u65b9\u6cd5\uff0c\u6216\u8005\u5b9a\u4e49\u4e86\u53ef\u4ee5\u652f\u6301\u4e0b\u6807\u7d22\u5f15\u7684
__getitem__<\/code>\u65b9\u6cd5(\u8fd9\u4e9b\u53cc\u4e0b\u5212\u7ebf\u65b9\u6cd5\u4f1a\u5728\u5176\u4ed6\u7ae0\u8282\u4e2d\u5168\u9762\u89e3\u91ca)\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u7b80\u5355\u8bf4\uff0c\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5c31\u662f\u80fd\u63d0\u4f9b\u8fed\u4ee3\u5668\u7684\u4efb\u610f\u5bf9\u8c61\u3002\u90a3\u8fed\u4ee3\u5668\u53c8\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n
\u8fed\u4ee3\u5668(Iterator)<\/h1>\n\n\n\n
next<\/code>(Python2) \u6216\u8005
__next__<\/code>\u65b9\u6cd5\uff0c\u5b83\u5c31\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u5c31\u8fd9\u4e48\u7b80\u5355\u3002\u73b0\u5728\u6211\u4eec\u6765\u7406\u89e3\u8fed\u4ee3(iteration)Next\u00a0<\/a>\u00a0Previous<\/a><\/p>\n\n\n\n
\u8fed\u4ee3(Iteration)<\/h1>\n\n\n\n
\u751f\u6210\u5668(Generators)<\/h1>\n\n\n\n
yield<\/code>(\u6682\u4e14\u8bd1\u4f5c\u201c\u751f\u51fa\u201d)\u4e00\u4e2a\u503c\u3002\u8fd9\u91cc\u6709\u4e2a\u751f\u6210\u5668\u51fd\u6570\u7684\u7b80\u5355\u4f8b\u5b50\uff1a<\/p>\n\n\n\n
def generator_function():\n for i in range(10):\n yield i\n\nfor item in generator_function():\n print(item)\n\n# Output: 0\n# 1\n# 2\n# 3\n# 4\n# 5\n# 6\n# 7\n# 8\n# 9\n<\/code><\/pre>\n\n\n\n
# generator version\ndef fibon(n):\n a = b = 1\n for i in range(n):\n yield a\n a, b = b, a + b\n<\/code><\/pre>\n\n\n\n
for x in fibon(1000000):\n print(x)\n<\/code><\/pre>\n\n\n\n
def fibon(n):\n a = b = 1\n result = []\n for i in range(n):\n result.append(a)\n a, b = b, a + b\n return result\n<\/code><\/pre>\n\n\n\n
next()<\/code>\u3002\u5b83\u5141\u8bb8\u6211\u4eec\u83b7\u53d6\u4e00\u4e2a\u5e8f\u5217\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u90a3\u6211\u4eec\u6765\u9a8c\u8bc1\u4e0b\u6211\u4eec\u7684\u7406\u89e3\uff1a<\/p>\n\n\n\n
def generator_function():\n for i in range(3):\n yield i\n\ngen = generator_function()\nprint(next(gen))\n# Output: 0\nprint(next(gen))\n# Output: 1\nprint(next(gen))\n# Output: 2\nprint(next(gen))\n# Output: Traceback (most recent call last):\n# File \"<stdin>\", line 1, in <module>\n# StopIteration\n<\/code><\/pre>\n\n\n\n
yield<\/code>\u6389\u6240\u6709\u7684\u503c\u540e\uff0c
next()<\/code>\u89e6\u53d1\u4e86\u4e00\u4e2a
StopIteration<\/code>\u7684\u5f02\u5e38\u3002\u57fa\u672c\u4e0a\u8fd9\u4e2a\u5f02\u5e38\u544a\u8bc9\u6211\u4eec\uff0c\u6240\u6709\u7684\u503c\u90fd\u5df2\u7ecf\u88ab
yield<\/code>\u5b8c\u4e86\u3002\u4f60\u4e5f\u8bb8\u4f1a\u5947\u602a\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u5728\u4f7f\u7528
for<\/code>\u5faa\u73af\u65f6\u6ca1\u6709\u8fd9\u4e2a\u5f02\u5e38\u5462\uff1f\u554a\u54c8\uff0c\u7b54\u6848\u5f88\u7b80\u5355\u3002
for<\/code>\u5faa\u73af\u4f1a\u81ea\u52a8\u6355\u6349\u5230\u8fd9\u4e2a\u5f02\u5e38\u5e76\u505c\u6b62\u8c03\u7528
next()<\/code>\u3002\u4f60\u77e5\u4e0d\u77e5\u9053Python\u4e2d\u4e00\u4e9b\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u4e5f\u652f\u6301\u8fed\u4ee3\u54e6\uff1f\u6211\u4eec\u8fd9\u5c31\u53bb\u770b\u770b\uff1a<\/p>\n\n\n\n
my_string = \"Yasoob\"\nnext(my_string)\n# Output: Traceback (most recent call last):\n# File \"<stdin>\", line 1, in <module>\n# TypeError: str object is not an iterator\n<\/code><\/pre>\n\n\n\n
str<\/code>\u5bf9\u8c61\u4e0d\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u5bf9\uff0c\u5c31\u662f\u8fd9\u6837\uff01\u5b83\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\u3002\u8fd9\u610f\u5473\u7740\u5b83\u652f\u6301\u8fed\u4ee3\uff0c\u4f46\u6211\u4eec\u4e0d\u80fd\u76f4\u63a5\u5bf9\u5176\u8fdb\u884c\u8fed\u4ee3\u64cd\u4f5c\u3002\u90a3\u6211\u4eec\u600e\u6837\u624d\u80fd\u5bf9\u5b83\u5b9e\u65bd\u8fed\u4ee3\u5462\uff1f\u662f\u65f6\u5019\u5b66\u4e60\u4e0b\u53e6\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c
iter<\/code>\u3002\u5b83\u5c06\u6839\u636e\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\u3002\u8fd9\u91cc\u662f\u6211\u4eec\u5982\u4f55\u4f7f\u7528\u5b83\uff1a<\/p>\n\n\n\n
my_string = \"Yasoob\"\nmy_iter = iter(my_string)\nnext(my_iter)\n# Output: 'Y'\n<\/code><\/pre>\n\n\n\n
<\/p>\n","protected":false},"excerpt":{"rendered":"