{"id":3637,"date":"2025-06-03T15:14:43","date_gmt":"2025-06-04T07:44:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3637"},"modified":"2025-06-04T15:14:43","modified_gmt":"2025-06-04T07:44:43","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%adstopiteration%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%adstopiteration%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e6%87%89%e7%94%a8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 StopIteration\uff1a\u529f\u80fd\u3001\u61c9\u7528\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, StopIteration, \u8fed\u4ee3\u5668, \u6559\u5b78, \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 StopIteration\uff1a\u529f\u80fd\u3001\u61c9\u7528\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0cStopIteration \u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u4f8b\u5916\uff0c\u5b83\u5141\u8a31\u6211\u5011\u5728\u8fed\u4ee3\u5668\u4e2d\u5b89\u5168\u5730\u505c\u6b62\u8fed\u4ee3\uff0c\u800c\u4e0d\u6703\u5c0e\u81f4\u7a0b\u5f0f\u5d29\u6f70\u3002\u9019\u500b\u529f\u80fd\u5c0d\u65bc\u8655\u7406\u5927\u91cf\u8cc7\u6599\u6216\u81ea\u5b9a\u7fa9\u8fed\u4ee3\u5668\u6642\u5c24\u70ba\u91cd\u8981\u3002\u672c\u6587\u5c07\u4ecb\u7d39 StopIteration \u7684\u57fa\u672c\u7528\u6cd5\u53ca\u5176\u5728\u5be6\u969b\u958b\u767c\u4e2d\u7684\u61c9\u7528\uff0c\u4e26\u63d0\u4f9b 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>StopIteration \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>StopIteration \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5728\u8fed\u4ee3\u5668\u4e2d\u505c\u6b62\u8fed\u4ee3\u3002\u7576\u8fed\u4ee3\u5668\u9054\u5230\u7d50\u5c3e\u6216\u67d0\u4e9b\u7279\u5b9a\u689d\u4ef6\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 StopIteration \u4f86\u63a7\u5236\u8fed\u4ee3\u5668\u7684\u884c\u70ba\uff0c\u9019\u6a23\u5c31\u4e0d\u9700\u8981\u4f9d\u8cf4\u5916\u90e8\u7684\u689d\u4ef6\u4f86\u7d50\u675f\u8fed\u4ee3\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5b9a\u7fa9\u4e00\u500b\u7c21\u55ae\u7684\u8fed\u4ee3\u5668\uff0c\u7576\u9047\u5230\u7279\u5b9a\u689d\u4ef6\u6642\u62cb\u51fa StopIteration\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u8fed\u4ee3\u5668<br \/>\ndef my_iterator():<br \/>\n    for i in range(10):<br \/>\n        if i == 5:<br \/>\n            raise StopIteration  # \u7576 i \u7b49\u65bc 5 \u6642\u505c\u6b62\u8fed\u4ee3<br \/>\n        yield i<\/p>\n<p># \u4f7f\u7528\u8fed\u4ee3\u5668<br \/>\nfor i in my_iterator():<br \/>\n    print(i)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u7576\u8fed\u4ee3\u5668\u9047\u5230\u6578\u5b57 5 \u6642\uff0c\u6703\u62cb\u51fa StopIteration\uff0c\u5f9e\u800c\u505c\u6b62\u8fed\u4ee3\u4e26\u907f\u514d\u4e86\u7a0b\u5f0f\u5d29\u6f70\u3002<\/p>\n<h2>StopIteration \u7684\u61c9\u7528<\/h2>\n<p>StopIteration \u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u8fed\u4ee3\u5668\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\uff0c\u4f8b\u5982\uff0c\u6211\u5011\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u51fd\u6578\u4f86\u6aa2\u67e5\u8fed\u4ee3\u5668\u4e2d\u662f\u5426\u5b58\u5728\u67d0\u500b\u5143\u7d20\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u8fed\u4ee3\u5668<br \/>\ndef my_iterator():<br \/>\n    for i in range(10):<br \/>\n        yield i<\/p>\n<p># \u6aa2\u67e5\u8fed\u4ee3\u5668\u4e2d\u662f\u5426\u6709\u7279\u5b9a\u7684\u5143\u7d20<br \/>\ndef check_element(iterator, element):<br \/>\n    try:<br \/>\n        while True:<br \/>\n            if next(iterator) == element:<br \/>\n                return True<br \/>\n    except StopIteration:<br \/>\n        return False<\/p>\n<p># \u4f7f\u7528\u8fed\u4ee3\u5668<br \/>\niterator = my_iterator()<br \/>\nprint(check_element(iterator, 5))  # \u8f38\u51fa: True<br \/>\nprint(check_element(iterator, 10)) # \u8f38\u51fa: False<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u6aa2\u67e5\u7279\u5b9a\u5143\u7d20\u7684\u51fd\u6578\uff0c\u7576\u8fed\u4ee3\u5668\u904d\u6b77\u5b8c\u6240\u6709\u5143\u7d20\u5f8c\u6703\u62cb\u51fa StopIteration\uff0c\u5f9e\u800c\u5b89\u5168\u7d50\u675f\u51fd\u6578\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 StopIteration \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\u672a\u8655\u7406\u7684 StopIteration \u4f8b\u5916\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u7a0b\u5f0f\u5d29\u6f70\u3002\u56e0\u6b64\uff0c\u5efa\u8b70\u5728\u4f7f\u7528 next() \u51fd\u6578\u6642\uff0c\u52d9\u5fc5\u5c07\u5176\u5305\u88dd\u5728 try-except \u5340\u584a\u4e2d\uff0c\u4ee5\u78ba\u4fdd\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>StopIteration \u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u57fa\u672c\u7684\u8fed\u4ee3\u5668\uff0c\u9084\u53ef\u4ee5\u8207\u5176\u4ed6 Python \u529f\u80fd\u7d50\u5408\u4f7f\u7528\uff0c\u5982\u81ea\u5b9a\u7fa9\u751f\u6210\u5668\u3001\u7570\u6b65\u7de8\u7a0b\u7b49\u3002\u5728\u9ad8\u968e\u61c9\u7528\u4e2d\uff0c\u4e86\u89e3 StopIteration \u7684\u904b\u4f5c\u539f\u7406\u53ef\u4ee5\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u8a2d\u8a08\u548c\u5be6\u73fe\u8907\u96dc\u7684\u8fed\u4ee3\u908f\u8f2f\u3002<\/p>\n<p>\u8981\u4e86\u89e3\u66f4\u591a\u95dc\u65bc Python \u7684\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668\u7684\u77e5\u8b58\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7 [Python \u8fed\u4ee3\u5668\u8207\u751f\u6210\u5668\u7684\u5b8c\u6574\u6559\u5b78](https:\/\/vocus.cc\/article\/640a3e77fd89780001b5ae8f)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 StopIteration \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8a31\u6211\u5011\u5b89\u5168\u5730\u505c\u6b62\u8fed\u4ee3\uff0c\u4e26\u4e14\u53ef\u4ee5\u7528\u65bc\u8655\u7406\u8fed\u4ee3\u5668\u4e2d\u7684\u8cc7\u6599\u3002\u7121\u8ad6\u662f\u5728\u57fa\u790e\u5b78\u7fd2\u9084\u662f\u5be6\u969b\u958b\u767c\u4e2d\uff0c\u6b63\u78ba\u7406\u89e3\u548c\u904b\u7528 StopIteration \u90fd\u662f\u63d0\u5347\u7a0b\u5f0f\u8a2d\u8a08\u80fd\u529b\u7684\u95dc\u9375\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: StopIteration \u662f\u4ec0\u9ebc\u6642\u5019\u88ab\u62cb\u51fa\u7684\uff1f**<br \/>\nA1: StopIteration \u5728\u8fed\u4ee3\u5668\u7684\u5143\u7d20\u7528\u5b8c\u6642\u88ab\u62cb\u51fa\uff0c\u901a\u5e38\u662f\u5728\u4f7f\u7528 next() \u51fd\u6578\u6642\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5b89\u5168\u5730\u4f7f\u7528 StopIteration\uff1f**<br \/>\nA2: \u4f7f\u7528 try-except \u5340\u584a\u4f86\u6355\u6349 StopIteration \u4f8b\u5916\uff0c\u4ee5\u9632\u6b62\u7a0b\u5f0f\u5d29\u6f70\u3002<\/p>\n<p>**Q3: StopIteration \u53ef\u4ee5\u7528\u5728\u54ea\u4e9b\u5834\u5408\uff1f**<br \/>\nA3: StopIteration \u5e38\u7528\u65bc\u81ea\u5b9a\u7fa9\u8fed\u4ee3\u5668\u3001\u751f\u6210\u5668\u4ee5\u53ca\u8655\u7406\u5927\u8cc7\u6599\u96c6\u7684\u60c5\u6cc1\u4e0b\uff0c\u4ee5\u4fbf\u5b89\u5168\u5730\u63a7\u5236\u8fed\u4ee3\u904e\u7a0b\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>StopIteration\u662fPython\u4e2d\u7684\u4e00\u500bmethods()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8b93\u7a0b\u5f0f\u505c\u6b62\u8fed\u4ee3\uff0c\u4e26\u4e14\u63d0\u4f9b\u4e00\u500b\u53ef\u4ee5\u63a7\u5236\u8fed\u4ee3\u7684\u65b9\u6cd5\uff0c\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39StopIteration\u7684\u529f\u80fd\u8207\u61c9\u7528\uff0c\u8b93\u4f60\u66f4\u52a0\u4e86\u89e3Python\u4e2d\u7684methods()\u51fd\u6578\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-WF","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3637"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3637\/revisions"}],"predecessor-version":[{"id":12772,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3637\/revisions\/12772"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}