{"id":204374,"date":"2025-05-29T13:46:29","date_gmt":"2025-05-29T05:46:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204374\/"},"modified":"2025-05-29T13:46:29","modified_gmt":"2025-05-29T05:46:29","slug":"python-%e5%b1%80%e9%83%a8%e5%8f%98%e9%87%8f%e4%bf%ae%e6%94%b9%e9%94%99%e8%af%af%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3unboundlocalerror%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204374\/","title":{"rendered":"Python \u5c40\u90e8\u53d8\u91cf\u4fee\u6539\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnboundLocalError\u201d\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5c40\u90e8\u53d8\u91cf\u4fee\u6539\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnboundLocalError\u201d\uff1f<\/h1>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300aPython \u5c40\u90e8\u53d8\u91cf\u4fee\u6539\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnboundLocalError\u201d\uff1f\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241106\/1730851238672ab1a6f0660.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u5c40\u90e8\u53d8\u91cf\u9519\u8bef\u5256\u6790<\/strong><\/p>\n<p>\u5f53\u5c1d\u8bd5\u4fee\u6539\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 &#8220;unboundlocalerror&#8221; \u9519\u8bef\u3002\u8fd9\u662f\u56e0\u4e3a python \u4e25\u683c\u533a\u5206\u5c40\u90e8\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u800c\u5c40\u90e8\u53d8\u91cf\u53ea\u5728\u51fd\u6570\u7684\u4f5c\u7528\u57df\u5185\u6709\u6548\u3002<\/p>\n<p>\u5728\u793a\u4f8b\u4ee3\u7801\u4e2d\uff1a<\/p>\n<p>&lt;pre class=&#8221;python&#8221;&gt;def f1():<\/p>\n<pre>i=1\ndef f2():\n    i=i+1&lt;\/code&gt;&lt;\/pre&gt;\n<\/pre>\n<p>f2() \u51fd\u6570\u8bd5\u56fe\u4fee\u6539 f1() \u51fd\u6570\u4e2d\u5b9a\u4e49\u7684\u5c40\u90e8\u53d8\u91cf i\uff0c\u4f46\u5b83\u65e0\u6cd5\u8bbf\u95ee\u8be5\u53d8\u91cf\u3002\u8fd9\u662f\u56e0\u4e3a\u5d4c\u5957\u51fd\u6570 f2() \u4e0d\u4f1a\u7ee7\u627f\u7236\u51fd\u6570\u5c40\u90e8\u53d8\u91cf\u7684\u4fee\u6539\u3002<\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u9519\u8bef\uff0c\u53ef\u4ee5\u5c06 i \u58f0\u660e\u4e3a\u975e\u5c40\u90e8\u53d8\u91cf\uff0c\u8fd9\u6837 f2() \u5c31\u53ef\u4ee5\u8bbf\u95ee\u5b83\u4e86\uff1a<\/p>\n<p>&lt;pre class=&#8221;python&#8221;&gt;def f1():<\/p>\n<pre>i=1\ndef f2():\n    nonlocal i\n    i=i+1&lt;\/code&gt;&lt;\/pre&gt;\n<\/pre>\n<p>nonlocal \u5173\u952e\u5b57\u544a\u8bc9 python \u5c06 i \u89c6\u4e3a\u7236\u51fd\u6570 f1() \u7684\u5c40\u90e8\u53d8\u91cf\u3002\u73b0\u5728\uff0cf2() \u53ef\u4ee5\u76f4\u63a5\u4fee\u6539 f1() \u4e2d\u7684 i\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPython \u5c40\u90e8\u53d8\u91cf\u4fee\u6539\u9519\u8bef\uff1a\u5982\u4f55\u89e3\u51b3\u201cUnboundLocalError\u201d\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5c40\u90e8\u53d8\u91cf\u4fee\u6539\u9519\u8bef\uff1a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204374","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204374"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204374\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}