{"id":3627,"date":"2025-06-01T14:48:35","date_gmt":"2025-06-04T07:40:35","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3627"},"modified":"2025-06-04T14:48:35","modified_gmt":"2025-06-04T07:40:35","slug":"%e8%a7%a3%e6%b1%bapython%e4%b8%adreferenceerror%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%adreferenceerror%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u89e3\u6c7a Python \u4e2d\u5e38\u898b\u7684 ReferenceError\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, ReferenceError, \u7a0b\u5f0f\u6559\u5b78, \u932f\u8aa4\u8655\u7406\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 ReferenceError<\/h1>\n<p>\u5728 Python \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\uff0cReferenceError \u662f\u4e00\u7a2e\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5b83\u8868\u793a\u7a0b\u5f0f\u78bc\u5f15\u7528\u4e86\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u540d\u7a31\u3002\u9019\u7a2e\u932f\u8aa4\u901a\u5e38\u662f\u7531\u65bc\u62fc\u5beb\u932f\u8aa4\u6216\u672a\u6b63\u78ba\u5f15\u7528\u67d0\u500b\u7269\u4ef6\u6216\u51fd\u5f0f\u800c\u5c0e\u81f4\u7684\u3002\u4e86\u89e3\u5982\u4f55\u8b58\u5225\u8207\u4fee\u6b63\u9019\u7a2e\u932f\u8aa4\uff0c\u5c0d\u65bc\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u7a69\u5b9a\u6027\u8207\u53ef\u8b80\u6027\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2>ReferenceError \u7684\u767c\u751f\u539f\u56e0<\/h2>\n<p>ReferenceError \u7684\u767c\u751f\u539f\u56e0\u4e3b\u8981\u6709\u4ee5\u4e0b\u5e7e\u7a2e\u60c5\u6cc1\uff1a<\/p>\n<p>1. **\u62fc\u5beb\u932f\u8aa4**\uff1a\u5f15\u7528\u7684\u8b8a\u6578\u6216\u51fd\u5f0f\u540d\u7a31\u62fc\u5beb\u932f\u8aa4\u3002<br \/>\n2. **\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578\u6216\u51fd\u5f0f**\uff1a\u5728\u7a0b\u5f0f\u4e2d\u5617\u8a66\u4f7f\u7528\u5c1a\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578\u6216\u51fd\u5f0f\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5169\u500b\u5178\u578b\u7bc4\u4f8b\uff1a<\/p>\n<p>**\u7bc4\u4f8b 1\uff1a\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578**<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u8b8a\u6578<br \/>\nx = 10<\/p>\n<p># \u5617\u8a66\u5f15\u7528\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578<br \/>\nprint(y)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u6703\u51fa\u73fe\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8220;`<br \/>\nNameError: name &#8216;y&#8217; is not defined<br \/>\n&#8220;`<\/p>\n<p>**\u7bc4\u4f8b 2\uff1a\u672a\u5b9a\u7fa9\u7684\u51fd\u5f0f**<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f<br \/>\ndef my_func():<br \/>\n    print(&#8220;Hello World!&#8221;)<\/p>\n<p># \u5617\u8a66\u5f15\u7528\u672a\u5b9a\u7fa9\u7684\u51fd\u5f0f<br \/>\nmy_func2()<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u6703\u51fa\u73fe\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8220;`<br \/>\nNameError: name &#8216;my_func2&#8217; is not defined<br \/>\n&#8220;`<\/p>\n<h2>\u5982\u4f55\u6709\u6548\u89e3\u6c7a ReferenceError<\/h2>\n<p>\u8981\u89e3\u6c7a ReferenceError\uff0c\u60a8\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6b65\u9a5f\uff1a<\/p>\n<p>1. **\u6aa2\u67e5\u62fc\u5beb**\uff1a\u4ed4\u7d30\u6aa2\u67e5\u5f15\u7528\u7684\u7269\u4ef6\u6216\u51fd\u5f0f\u540d\u7a31\u662f\u5426\u6b63\u78ba\u62fc\u5beb\u3002<br \/>\n2. **\u78ba\u8a8d\u5b9a\u7fa9**\uff1a\u78ba\u4fdd\u5728\u5f15\u7528\u4e4b\u524d\uff0c\u8b8a\u6578\u6216\u51fd\u5f0f\u5df2\u88ab\u6b63\u78ba\u5b9a\u7fa9\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5f15\u7528\u4e86\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578\uff0c\u60a8\u53ef\u4ee5\u9019\u6a23\u4fee\u6b63\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5b9a\u7fa9\u4e00\u500b\u8b8a\u6578<br \/>\nx = 10<\/p>\n<p># \u5b9a\u7fa9\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578<br \/>\ny = 20<\/p>\n<p># \u73fe\u5728\u5f15\u7528\u8b8a\u6578 y<br \/>\nprint(y)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u6703\u6b63\u5e38\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\n20<br \/>\n&#8220;`<\/p>\n<h2>\u9032\u4e00\u6b65\u7684\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u7576\u60a8\u9047\u5230 ReferenceError \u6642\uff0c\u9664\u4e86\u6aa2\u67e5\u62fc\u5beb\u8207\u5b9a\u7fa9\u5916\uff0c\u9084\u53ef\u4ee5\u8003\u616e\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>&#8211; \u4f7f\u7528 Python \u7684\u5167\u5efa\u51fd\u5f0f `globals()` \u548c `locals()` \u4f86\u6aa2\u67e5\u7576\u524d\u7bc4\u570d\u5167\u7684\u8b8a\u6578\u3002<br \/>\n&#8211; \u4f7f\u7528 IDE \u7684\u8abf\u8a66\u5de5\u5177\u4f86\u9010\u884c\u6aa2\u67e5\u7a0b\u5f0f\u78bc\uff0c\u4ee5\u627e\u51fa\u932f\u8aa4\u4f86\u6e90\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5efa\u8b70\u5c07\u932f\u8aa4\u8655\u7406\u8207\u65e5\u8a8c\u8a18\u9304\u7d50\u5408\uff0c\u9019\u6a23\u53ef\u4ee5\u5728\u7a0b\u5f0f\u51fa\u73fe\u932f\u8aa4\u6642\uff0c\u8f15\u9b06\u8ffd\u8e64\u932f\u8aa4\u767c\u751f\u7684\u5730\u65b9\u3002<\/p>\n<p>\u82e5\u60a8\u60f3\u6df1\u5165\u4e86\u89e3 Python \u932f\u8aa4\u8207\u7570\u5e38\u7684\u8655\u7406\u65b9\u5f0f\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7 [Python \u932f\u8aa4\u8207\u7570\u5e38\u8655\u7406\u6559\u5b78](https:\/\/miner.tw\/python-error-handling)\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python \u7684 ReferenceError \u662f\u4e00\u7a2e\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5b83\u901a\u5e38\u7531\u65bc\u62fc\u5beb\u932f\u8aa4\u6216\u672a\u6b63\u78ba\u5f15\u7528\u7269\u4ef6\u6216\u51fd\u5f0f\u5c0e\u81f4\u3002\u900f\u904e\u6b63\u78ba\u7684\u932f\u8aa4\u6392\u9664\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\u4e26\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u8cea\u91cf\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u5982\u4f55\u6aa2\u67e5\u8b8a\u6578\u662f\u5426\u5df2\u5b9a\u7fa9\uff1f**<br \/>\nA1: \u60a8\u53ef\u4ee5\u4f7f\u7528 `globals()` \u6216 `locals()` \u51fd\u5f0f\u4f86\u6aa2\u67e5\u7576\u524d\u4f5c\u7528\u57df\u5167\u6709\u54ea\u4e9b\u8b8a\u6578\u5df2\u88ab\u5b9a\u7fa9\u3002<\/p>\n<p>**Q2: \u5982\u679c\u5f15\u7528\u7684\u51fd\u5f0f\u672a\u5b9a\u7fa9\uff0c\u6211\u8a72\u600e\u9ebc\u8fa6\uff1f**<br \/>\nA2: \u78ba\u4fdd\u8a72\u51fd\u5f0f\u5df2\u7d93\u5728\u5f15\u7528\u4e4b\u524d\u5b9a\u7fa9\uff0c\u4e26\u6aa2\u67e5\u51fd\u5f0f\u540d\u7a31\u7684\u62fc\u5beb\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<p>**Q3: \u6709\u4ec0\u9ebc\u5de5\u5177\u53ef\u4ee5\u5354\u52a9\u6211\u9032\u884c Python \u7a0b\u5f0f\u7684\u932f\u8aa4\u6392\u9664\uff1f**<br \/>\nA3: \u60a8\u53ef\u4ee5\u4f7f\u7528 Python \u7684\u5167\u5efa\u8abf\u8a66\u5de5\u5177\u5982 `pdb`\uff0c\u6216\u662f\u4f7f\u7528 IDE \u4e2d\u7684\u8abf\u8a66\u529f\u80fd\u4f86\u9010\u884c\u6aa2\u67e5\u7a0b\u5f0f\u78bc\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Python\u4e2d\uff0cReferenceError\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u5b83\u8868\u793a\u7a0b\u5e8f\u5617\u8a66\u5f15\u7528\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u8b8a\u91cf\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u89e3\u6c7aReferenceError\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\uff0c\u4ee5\u4fbf\u60a8\u80fd\u5920\u66f4\u6709\u6548\u5730\u4f7f\u7528Python\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-3627","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\/3627","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=3627"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3627\/revisions"}],"predecessor-version":[{"id":12776,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3627\/revisions\/12776"}],"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=3627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}