{"id":3653,"date":"2025-06-03T15:26:06","date_gmt":"2025-06-04T07:54:05","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3653"},"modified":"2025-06-04T15:26:06","modified_gmt":"2025-06-04T07:54:05","slug":"%e8%a7%a3%e6%b1%bapython%e4%b8%adunboundlocalerror%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e8%a7%a3%e6%b1%bapython%e4%b8%adunboundlocalerror%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u5168\u9762\u89e3\u6790\uff1a\u89e3\u6c7a Python \u4e2d UnboundLocalError \u7684\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, UnboundLocalError, Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>\u4ec0\u9ebc\u662f Python \u7684 UnboundLocalError\uff1f<\/h1>\n<p>\u5728 Python \u7a0b\u5f0f\u4e2d\uff0cUnboundLocalError \u662f\u4e00\u500b\u5e38\u898b\u7684\u57f7\u884c\u932f\u8aa4\uff0c\u7576\u60a8\u5728\u7a0b\u5f0f\u78bc\u4e2d\u5f15\u7528\u672a\u5b9a\u7fa9\u7684\u672c\u5730\u8b8a\u6578\u6642\uff0c\u5c31\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u9019\u7a2e\u932f\u8aa4\u901a\u5e38\u767c\u751f\u5728\u51fd\u6578\u6216\u65b9\u6cd5\u5167\u90e8\uff0c\u8b93\u8a31\u591a\u958b\u767c\u8005\u611f\u5230\u56f0\u60d1\u3002<\/p>\n<h2>UnboundLocalError \u7684\u539f\u56e0<\/h2>\n<p>UnboundLocalError \u7684\u7522\u751f\u6e90\u65bc Python \u7684\u4f5c\u7528\u57df\u898f\u5247\u3002\u6839\u64da\u9019\u4e9b\u898f\u5247\uff0c\u5982\u679c\u60a8\u5728\u51fd\u6578\u6216\u65b9\u6cd5\u5167\u90e8\u5b9a\u7fa9\u4e86\u4e00\u500b\u8b8a\u6578\uff0c\u90a3\u9ebc\u8a72\u8b8a\u6578\u53ea\u80fd\u5728\u8a72\u7bc4\u570d\u5167\u4f7f\u7528\u3002\u7576\u60a8\u8a66\u5716\u5f15\u7528\u4e00\u500b\u5c1a\u672a\u88ab\u5b9a\u7fa9\u7684\u672c\u5730\u8b8a\u6578\u6642\uff0cPython \u5c07\u5f15\u767c UnboundLocalError\u3002<\/p>\n<h2>\u5982\u4f55\u6709\u6548\u89e3\u6c7a UnboundLocalError<\/h2>\n<p>\u8981\u89e3\u6c7a UnboundLocalError\uff0c\u60a8\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u5e7e\u7a2e\u65b9\u6cd5\uff1a<\/p>\n<p>1. **\u78ba\u4fdd\u8b8a\u6578\u5df2\u5b9a\u7fa9**\uff1a\u5728\u4f7f\u7528\u8b8a\u6578\u4e4b\u524d\uff0c\u8acb\u6aa2\u67e5\u8a72\u8b8a\u6578\u662f\u5426\u5df2\u7d93\u88ab\u5b9a\u7fa9\u3002<\/p>\n<p>2. **\u4f7f\u7528 `global` \u95dc\u9375\u5b57**\uff1a\u5982\u679c\u60a8\u5e0c\u671b\u5728\u51fd\u6578\u4e2d\u4fee\u6539\u5168\u5c40\u8b8a\u6578\uff0c\u53ef\u4ee5\u4f7f\u7528 `global` \u95dc\u9375\u5b57\u4f86\u660e\u78ba\u544a\u8a34 Python \u8a72\u8b8a\u6578\u662f\u5168\u5c40\u7684\u3002<\/p>\n<p>3. **\u4f7f\u7528 `nonlocal` \u95dc\u9375\u5b57**\uff1a\u5982\u679c\u60a8\u5728\u5d4c\u5957\u51fd\u6578\u4e2d\u60f3\u8981\u4fee\u6539\u5916\u5c64\u51fd\u6578\u7684\u8b8a\u6578\uff0c\u53ef\u4ee5\u4f7f\u7528 `nonlocal` \u95dc\u9375\u5b57\u3002<\/p>\n<h2>\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u7bc4\u7a0b\u5f0f\u78bc\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `global` \u548c `nonlocal` \u95dc\u9375\u5b57\u4f86\u89e3\u6c7a UnboundLocalError\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u5168\u5c40\u8b8a\u6578<br \/>\nx = 10<\/p>\n<p>def foo():<br \/>\n    # \u4f7f\u7528 global \u95dc\u9375\u5b57\u5c07 x \u5b9a\u7fa9\u70ba\u5168\u5c40\u8b8a\u6578<br \/>\n    global x<br \/>\n    x = 20<\/p>\n<p>def outer():<br \/>\n    y = 30<br \/>\n    def inner():<br \/>\n        # \u4f7f\u7528 nonlocal \u95dc\u9375\u5b57\u5c07 y \u5b9a\u7fa9\u70ba\u975e\u672c\u5730\u8b8a\u6578<br \/>\n        nonlocal y<br \/>\n        y = 40<br \/>\n    inner()<br \/>\n    return y<\/p>\n<p>foo()<br \/>\nprint(x)  # \u8f38\u51fa\uff1a20<\/p>\n<p>result = outer()<br \/>\nprint(result)  # \u8f38\u51fa\uff1a40<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c`foo` \u51fd\u6578\u6210\u529f\u4fee\u6539\u4e86\u5168\u5c40\u8b8a\u6578 `x`\uff0c\u800c `outer` \u51fd\u6578\u4e2d\u7684 `inner` \u51fd\u6578\u5247\u4f7f\u7528 `nonlocal` \u4f86\u4fee\u6539\u5916\u5c64\u51fd\u6578\u7684\u8b8a\u6578 `y`\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\u8207\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u969b\u958b\u767c\u4e2d\uff0cUnboundLocalError \u5e38\u5e38\u662f\u7531\u65bc\u8b8a\u6578\u547d\u540d\u4e0d\u7576\u6216\u8005\u4f5c\u7528\u57df\u6df7\u6dc6\u9020\u6210\u7684\u3002\u70ba\u4e86\u907f\u514d\u6b64\u932f\u8aa4\uff0c\u5efa\u8b70\u60a8\uff1a<\/p>\n<p>&#8211; \u78ba\u4fdd\u8b8a\u6578\u547d\u540d\u6e05\u6670\uff0c\u907f\u514d\u8207\u5168\u5c40\u8b8a\u6578\u91cd\u540d\u3002<br \/>\n&#8211; \u4f7f\u7528 IDE \u6216\u7de8\u8f2f\u5668\u7684\u975c\u614b\u4ee3\u78bc\u6aa2\u67e5\u529f\u80fd\uff0c\u53ca\u65e9\u767c\u73fe\u4f5c\u7528\u57df\u554f\u984c\u3002<br \/>\n&#8211; \u6e2c\u8a66\u51fd\u6578\u7684\u5404\u7a2e\u60c5\u6cc1\uff0c\u78ba\u4fdd\u6240\u6709\u908a\u754c\u689d\u4ef6\u90fd\u88ab\u8003\u616e\u5230\u3002<\/p>\n<p>\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python \u8b8a\u6578\u4f5c\u7528\u57df\u7684\u6982\u5ff5\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7 [Python \u8b8a\u6578\u4f5c\u7528\u57df\u8207\u751f\u547d\u9031\u671f\u6559\u5b78](https:\/\/vocus.cc\/article\/your-article-link)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 UnboundLocalError \u662f\u4e00\u500b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4e3b\u8981\u7531\u65bc\u8b8a\u6578\u672a\u5b9a\u7fa9\u6216\u4f5c\u7528\u57df\u554f\u984c\u5f15\u8d77\u3002\u900f\u904e\u4f7f\u7528 `global` \u548c `nonlocal` \u95dc\u9375\u5b57\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u6b64\u932f\u8aa4\uff0c\u4e26\u5728\u51fd\u6578\u4e2d\u6b63\u78ba\u4f7f\u7528\u8b8a\u6578\u3002\u96a8\u8457 Python \u8a9e\u6cd5\u7684\u6f14\u9032\uff0c\u4fdd\u6301\u5c0d\u6700\u65b0\u6700\u4f73\u5be6\u8e10\u7684\u4e86\u89e3\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: UnboundLocalError \u5982\u4f55\u8207\u5176\u4ed6\u932f\u8aa4\u5340\u5206\uff1f**<br \/>\nA1: UnboundLocalError \u7279\u5225\u91dd\u5c0d\u5f15\u7528\u672a\u5b9a\u7fa9\u672c\u5730\u8b8a\u6578\u7684\u60c5\u6cc1\uff0c\u8207 NameError \u4e0d\u540c\uff0c\u5f8c\u8005\u662f\u56e0\u70ba\u5f15\u7528\u4e86\u672a\u5b9a\u7fa9\u7684\u5168\u5c40\u8b8a\u6578\u3002<\/p>\n<p>**Q2: \u4f7f\u7528 global \u548c nonlocal \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA2: `global` \u7528\u65bc\u8a2a\u554f\u5168\u5c40\u8b8a\u6578\uff0c\u800c `nonlocal` \u7528\u65bc\u8a2a\u554f\u5916\u5c64\u51fd\u6578\u7684\u8b8a\u6578\u3002\u9019\u5169\u8005\u7684\u4f5c\u7528\u7bc4\u570d\u4e0d\u540c\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u907f\u514d UnboundLocalError\uff1f**<br \/>\nA3: \u78ba\u4fdd\u5728\u4f7f\u7528\u8b8a\u6578\u524d\u5df2\u6b63\u78ba\u5b9a\u7fa9\uff0c\u4e26\u4e14\u6ce8\u610f\u8b8a\u6578\u7684\u4f5c\u7528\u57df\u548c\u547d\u540d\uff0c\u4ee5\u907f\u514d\u91cd\u540d\u554f\u984c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UnboundLocalError\u662fPython\u4e2d\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5b83\u6703\u5c0e\u81f4\u7a0b\u5e8f\u5d29\u6f70\uff0c\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aUnboundLocalError\uff0c\u4ee5\u53ca\u5982\u4f55\u907f\u514d\u51fa\u73fe\u6b64\u932f\u8aa4\uff0c\u8b93\u4f60\u7684Python\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ef\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-3653","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-WV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3653","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=3653"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3653\/revisions"}],"predecessor-version":[{"id":3654,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3653\/revisions\/3654"}],"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=3653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}