{"id":204529,"date":"2025-05-29T14:55:59","date_gmt":"2025-05-29T06:55:59","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204529\/"},"modified":"2025-05-29T14:55:59","modified_gmt":"2025-05-29T06:55:59","slug":"python-%e5%b5%8c%e5%a5%97%e5%87%bd%e6%95%b0%e4%b8%ad%e5%bc%95%e7%94%a8%e5%b1%80%e9%83%a8%e5%8f%98%e9%87%8f%e6%97%b6%ef%bc%8c%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e5%87%ba%e7%8e%b0-unboundlocalerror","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204529\/","title":{"rendered":"Python \u5d4c\u5957\u51fd\u6570\u4e2d\u5f15\u7528\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 UnboundLocalError\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python \u5d4c\u5957\u51fd\u6570\u4e2d\u5f15\u7528\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 UnboundLocalError\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPython \u5d4c\u5957\u51fd\u6570\u4e2d\u5f15\u7528\u5c40\u90e8\u53d8\u91cf\u65f6\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0 UnboundLocalError\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241109\/1731164446672f791e3a392.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u7406\u89e3 python \u4e2d\u5c40\u90e8\u53d8\u91cf\u7684\u9519\u8bef<\/strong><\/p>\n<p>\u5f53\u5728\u5d4c\u5957\u51fd\u6570\u4e2d\u5f15\u7528\u5c40\u90e8\u53d8\u91cf\u65f6\uff0cpython \u53ef\u80fd\u5f15\u53d1\u4e00\u4e2a\u9519\u8bef\uff1aunboundlocalerror: local variable &#8216;\u53d8\u91cf\u540d&#8217; referenced before assignment\u3002<\/p>\n<p>\u50cf\u4ee5\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre>def f1():\n    i = 1\n    def f2():\n        i = i + 1<\/pre>\n<p>\u5f53\u6267\u884c f2() \u65f6\uff0c\u5b83\u5c06\u629b\u51fa unboundlocalerror\u3002\u8fd9\u662f\u56e0\u4e3a f2() \u4e2d\u7684 i \u88ab\u89e3\u91ca\u4e3a\u4e00\u4e2a\u65b0\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u800c\u6ca1\u6709\u5f15\u7528\u7236\u51fd\u6570 f1() \u4e2d\u540c\u540d\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u672c\u8d28\u4e0a\uff0cpython \u65e0\u6cd5\u5f04\u6e05\u695a f2 \u4e2d\u7684 i \u662f\u5426\u662f f1 \u4e2d\u7684 i\uff0c\u4ece\u800c\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4e4b\u4e00\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528\u5217\u8868\u5bf9\u8c61\uff1a<\/strong>\u5c06 i \u58f0\u660e\u4e3a\u4e00\u4e2a\u5217\u8868\u5bf9\u8c61\uff0c\u5982 i = [1]\u3002\u8fd9\u5c06\u5141\u8bb8\u5b50\u51fd\u6570\u4fee\u6539\u5217\u8868\u7684\u5143\u7d20\uff0c\u4f46\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539\u5217\u8868\u672c\u8eab\u3002<\/li>\n<li><strong>\u4f7f\u7528 nonlocal \u5173\u952e\u5b57\uff08python 3\uff09\uff1a<\/strong>\u5728 python 3 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 nonlocal \u5173\u952e\u5b57\u58f0\u660e\u5b50\u51fd\u6570\u4e2d\u7684\u53d8\u91cf\u5f15\u7528\u7236\u51fd\u6570\u7684\u540c\u540d\u5c40\u90e8\u53d8\u91cf\uff0c\u5141\u8bb8\u5176\u76f4\u63a5\u4fee\u6539\u8be5\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre># \u4f7f\u7528\u5217\u8868\u5bf9\u8c61\ndef f1():\n    i = [1]\n    def f2():\n        i[0] = i[0] + 1\n\n# \u4f7f\u7528 nonlocal \u5173\u952e\u5b57\uff08Python 3\uff09\ndef f1():\n    i = 1\n    def f2():\n        nonlocal i\n        i = i + 1<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u5d4c\u5957\u51fd\u6570\u4e2d\u5f15\u7528\u5c40\u90e8&#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-204529","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204529","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=204529"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204529\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}