{"id":2757,"date":"2025-03-14T22:44:15","date_gmt":"2025-03-14T14:44:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2757"},"modified":"2025-03-14T22:44:15","modified_gmt":"2025-03-14T14:44:15","slug":"python3-%e8%bf%ad%e4%bb%a3%e5%99%a8%e4%b8%8e%e7%94%9f%e6%88%90%e5%99%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/14\/python3-%e8%bf%ad%e4%bb%a3%e5%99%a8%e4%b8%8e%e7%94%9f%e6%88%90%e5%99%a8%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python3 \u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668\u8be6\u89e3"},"content":{"rendered":"\n<p>\u5728 Python \u4e2d\uff0c<strong>\u8fed\u4ee3\u5668\uff08Iterator\uff09<\/strong> \u548c <strong>\u751f\u6210\u5668\uff08Generator\uff09<\/strong> \u662f\u5904\u7406\u6570\u636e\u6d41\u548c\u96c6\u5408\u7684\u91cd\u8981\u5de5\u5177\u3002\u7406\u89e3\u5b83\u4eec\u7684\u6982\u5ff5\u53ca\u5176\u533a\u522b\uff0c\u5c06\u5e2e\u52a9\u6211\u4eec\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u8282\u7701\u5185\u5b58\u5e76\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u8fed\u4ee3\u5668\uff08Iterator\uff09<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u8fed\u4ee3\u5668\u662f\u4ec0\u4e48\uff1f<\/strong><\/h3>\n\n\n\n<p>\u8fed\u4ee3\u5668\u662f\u5b9e\u73b0\u4e86\u8fed\u4ee3\u534f\u8bae\u7684\u5bf9\u8c61\u3002\u8fed\u4ee3\u534f\u8bae\u6307\u7684\u662f <strong><code>__iter__()<\/code><\/strong> \u548c <strong><code>__next__()<\/code><\/strong> \u65b9\u6cd5\u3002\u6240\u6709\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u3001\u96c6\u5408\u7b49\uff09\u90fd\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u8fdb\u884c\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p><strong>\u8fed\u4ee3\u5668\u534f\u8bae<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>__iter__()<\/code><\/strong>\uff1a\u8fd4\u56de\u8fed\u4ee3\u5668\u5bf9\u8c61\u672c\u8eab\u3002<\/li>\n\n\n\n<li><strong><code>__next__()<\/code><\/strong>\uff1a\u8fd4\u56de\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u5982\u679c\u6ca1\u6709\u66f4\u591a\u5143\u7d20\uff0c\u629b\u51fa <code>StopIteration<\/code> \u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u8fed\u4ee3\u5668\u7684\u521b\u5efa<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u521b\u5efa\u8fed\u4ee3\u5668<\/strong><\/h4>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7 <code>iter()<\/code> \u51fd\u6570\u5c06\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5668\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>lst = &#91;1, 2, 3]\niterator = iter(lst)\nprint(next(iterator))  # 1\nprint(next(iterator))  # 2\nprint(next(iterator))  # 3\n# print(next(iterator))  # StopIteration \u5f02\u5e38<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 \u8fed\u4ee3\u5668\u7684\u7279\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fed\u4ee3\u5668\u662f <strong>\u60f0\u6027\u6c42\u503c<\/strong>\uff0c\u5373\u6bcf\u6b21\u8c03\u7528 <code>next()<\/code> \u8fd4\u56de\u4e00\u4e2a\u5143\u7d20\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u8fd4\u56de\u6240\u6709\u5143\u7d20\u3002<\/li>\n\n\n\n<li>\u8fed\u4ee3\u5668\u5728\u6ca1\u6709\u5143\u7d20\u65f6\u4f1a\u629b\u51fa <code>StopIteration<\/code> \u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u751f\u6210\u5668\uff08Generator\uff09<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u751f\u6210\u5668\u662f\u4ec0\u4e48\uff1f<\/strong><\/h3>\n\n\n\n<p>\u751f\u6210\u5668\u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u5b83\u662f\u901a\u8fc7 <strong>\u751f\u6210\u5668\u51fd\u6570<\/strong>\uff08\u4f7f\u7528 <code>yield<\/code> \u5173\u952e\u5b57\uff09\u6216 <strong>\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong> \u521b\u5efa\u7684\u3002\u751f\u6210\u5668\u662f\u60f0\u6027\u6c42\u503c\u7684\uff0c\u53ea\u5728\u9700\u8981\u65f6\u751f\u6210\u6570\u636e\uff0c\u56e0\u6b64\u5b83\u4eec\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\u6216\u6d41\u5f0f\u6570\u636e\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u751f\u6210\u5668\u7684\u521b\u5efa<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2.1 \u751f\u6210\u5668\u51fd\u6570<\/strong><\/h4>\n\n\n\n<p>\u751f\u6210\u5668\u51fd\u6570\u4e0e\u666e\u901a\u51fd\u6570\u7684\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\uff0c\u5b83\u5305\u542b <strong><code>yield<\/code> \u5173\u952e\u5b57<\/strong>\u3002\u6bcf\u6b21\u9047\u5230 <code>yield<\/code> \u65f6\uff0c\u51fd\u6570\u4f1a\u6682\u505c\u6267\u884c\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u4e0b\u4e00\u6b21\u8c03\u7528\u65f6\uff0c\u51fd\u6570\u4f1a\u4ece\u4e0a\u6b21 <code>yield<\/code> \u5904\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def my_generator():\n    yield 1\n    yield 2\n    yield 3\n\ngen = my_generator()\nprint(next(gen))  # 1\nprint(next(gen))  # 2\nprint(next(gen))  # 3\n# print(next(gen))  # StopIteration \u5f02\u5e38<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2.2.2 \u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/strong><\/h4>\n\n\n\n<p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u7c7b\u4f3c\u4e8e\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u4f46\u5b83\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u5217\u8868\u3002\u5b83\u4f7f\u7528\u5c0f\u62ec\u53f7 <code>()<\/code> \u6765\u521b\u5efa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gen_expr = (x ** 2 for x in range(3))\nprint(next(gen_expr))  # 0\nprint(next(gen_expr))  # 1\nprint(next(gen_expr))  # 4\n# print(next(gen_expr))  # StopIteration \u5f02\u5e38<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u751f\u6210\u5668\u7684\u7279\u70b9<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u60f0\u6027\u6c42\u503c<\/strong>\uff1a\u751f\u6210\u5668\u6bcf\u6b21\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u76f4\u5230\u5b8c\u5168\u8017\u5c3d\u3002<\/li>\n\n\n\n<li><strong>\u8282\u7701\u5185\u5b58<\/strong>\uff1a\u751f\u6210\u5668\u4e0d\u4f1a\u4e00\u6b21\u6027\u521b\u5efa\u6240\u6709\u7684\u503c\uff0c\u800c\u662f\u6309\u9700\u751f\u6210\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u652f\u6301\u8fed\u4ee3<\/strong>\uff1a\u751f\u6210\u5668\u53ef\u4ee5\u7528\u4e8e <code>for<\/code> \u5faa\u73af\u7b49\u8fed\u4ee3\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u53ea\u80fd\u904d\u5386\u4e00\u6b21<\/strong>\uff1a\u751f\u6210\u5668\u4e00\u65e6\u6d88\u8017\u5b8c\u6570\u636e\uff0c\u4e0d\u80fd\u91cd\u65b0\u5f00\u59cb\u8fed\u4ee3\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668\u7684\u533a\u522b<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u8fed\u4ee3\u5668<\/th><th>\u751f\u6210\u5668<\/th><\/tr><\/thead><tbody><tr><td><strong>\u5b9a\u4e49<\/strong><\/td><td>\u5b9e\u73b0\u4e86\u8fed\u4ee3\u534f\u8bae\uff08<code>__iter__()<\/code> \u548c <code>__next__()<\/code>\uff09<\/td><td>\u901a\u8fc7 <code>yield<\/code> \u5173\u952e\u5b57\u6216\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa<\/td><\/tr><tr><td><strong>\u521b\u5efa\u65b9\u5f0f<\/strong><\/td><td>\u901a\u8fc7 <code>iter()<\/code> \u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u4e3a\u8fed\u4ee3\u5668<\/td><td>\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u6216\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/td><\/tr><tr><td><strong>\u5185\u5b58\u4f7f\u7528<\/strong><\/td><td>\u521b\u5efa\u65f6\u751f\u6210\u6574\u4e2a\u5bf9\u8c61<\/td><td>\u6309\u9700\u751f\u6210\uff0c\u8282\u7701\u5185\u5b58<\/td><\/tr><tr><td><strong>\u662f\u5426\u652f\u6301\u4e00\u6b21\u6027\u904d\u5386<\/strong><\/td><td>\u53ef\u4ee5\u91cd\u65b0\u521b\u5efa\u8fed\u4ee3\u5668\u8fdb\u884c\u904d\u5386<\/td><td>\u4e0d\u652f\u6301\u91cd\u65b0\u904d\u5386\uff0c\u5df2\u8017\u5c3d\u540e\u4e0d\u80fd\u91cd\u7528<\/td><\/tr><tr><td><strong>\u6267\u884c\u65b9\u5f0f<\/strong><\/td><td>\u9010\u4e2a\u5143\u7d20\u8fd4\u56de<\/td><td>\u6bcf\u6b21 <code>yield<\/code> \u6682\u505c\u5e76\u8fd4\u56de\u4e00\u4e2a\u503c<\/td><\/tr><tr><td><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/td><td>\u9002\u7528\u4e8e\u9700\u8981\u591a\u6b21\u8bbf\u95ee\u6570\u636e\u7684\u573a\u666f<\/td><td>\u9002\u7528\u4e8e\u6d41\u5f0f\u6570\u636e\u3001\u8282\u7701\u5185\u5b58\u7684\u5927\u6570\u636e\u573a\u666f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u8fed\u4ee3\u5668\u4e0e\u751f\u6210\u5668\u7684\u5e94\u7528\u5b9e\u4f8b<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u8fed\u4ee3\u5668\u7684\u5e94\u7528\uff1a\u8bfb\u53d6\u5927\u6587\u4ef6<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u9010\u884c\u8bfb\u53d6\u5927\u6587\u4ef6\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\uff0c\u8282\u7701\u5185\u5b58\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def read_large_file(file_name):\n    with open(file_name, 'r') as file:\n        for line in file:\n            yield line.strip()  # \u4f7f\u7528 yield \u9010\u884c\u8fd4\u56de\n\nfor line in read_large_file('large_file.txt'):\n    print(line)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u751f\u6210\u5668\u7684\u5e94\u7528\uff1a\u751f\u6210\u65e0\u9650\u5e8f\u5217<\/strong><\/h3>\n\n\n\n<p>\u751f\u6210\u5668\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u65e0\u9650\u5e8f\u5217\uff0c\u4f8b\u5982\u6590\u6ce2\u90a3\u5951\u6570\u5217\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def fibonacci():\n    a, b = 0, 1\n    while True:\n        yield a\n        a, b = b, a + b\n\nfib = fibonacci()\nfor i in range(10):\n    print(next(fib))  # 0 1 1 2 3 5 8 13 21 34<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8fed\u4ee3\u5668<\/strong> \u662f\u9075\u5faa\u8fed\u4ee3\u534f\u8bae\u7684\u5bf9\u8c61\uff0c\u9002\u5408\u7528\u4e8e\u904d\u5386\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><strong>\u751f\u6210\u5668<\/strong> \u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8fed\u4ee3\u5668\uff0c\u901a\u8fc7 <code>yield<\/code> \u521b\u5efa\uff0c\u7528\u4e8e\u6309\u9700\u751f\u6210\u6570\u636e\uff0c\u5177\u6709\u8282\u7701\u5185\u5b58\u7684\u4f18\u52bf\u3002<\/li>\n\n\n\n<li><strong>\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668<\/strong> \u90fd\u652f\u6301\u60f0\u6027\u6c42\u503c\uff0c\u53ea\u6709\u5728\u9700\u8981\u65f6\u624d\u4f1a\u8ba1\u7b97\u5e76\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668\u5728\u6570\u636e\u6d41\u5904\u7406\u3001\u5927\u89c4\u6a21\u6570\u636e\u96c6\u7684\u904d\u5386\u548c\u8282\u7701\u5185\u5b58\u65b9\u9762\u975e\u5e38\u6709\u7528\u3002\u638c\u63e1\u5b83\u4eec\u80fd\u5e2e\u52a9\u4f60\u7f16\u5199\u9ad8\u6548\u4e14\u5185\u5b58\u53cb\u597d\u7684 Python \u7a0b\u5e8f\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Python \u4e2d\uff0c\u8fed\u4ee3\u5668\uff08Iterator\uff09 \u548c \u751f\u6210\u5668\uff08Generator\uff09 \u662f\u5904\u7406\u6570\u636e\u6d41\u548c\u96c6\u5408\u7684\u91cd\u8981\u5de5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2758,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-2757","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-3-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2757","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2757"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2757\/revisions"}],"predecessor-version":[{"id":2759,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2757\/revisions\/2759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/2758"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}