{"id":2994,"date":"2025-06-02T16:41:03","date_gmt":"2025-06-04T09:12:03","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2994"},"modified":"2025-06-04T16:41:03","modified_gmt":"2025-06-04T09:12:03","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84exec%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84exec%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3Python\u4e2d\u7684exec()\u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, exec(), Python \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h1>Python exec()\u51fd\u6578\u4ecb\u7d39<\/h1>\n<p>Python\u7684exec()\u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u57f7\u884c\u52d5\u614b\u751f\u6210\u7684Python\u7a0b\u5f0f\u78bc\u3002\u7121\u8ad6\u662f\u57f7\u884c\u5b57\u4e32\u4e2d\u7684Python\u8a9e\u53e5\uff0c\u9084\u662f\u8b80\u53d6\u4e26\u57f7\u884c\u5916\u90e8\u7684Python\u6a94\u6848\uff0cexec()\u51fd\u6578\u90fd\u80fd\u8f15\u9b06\u61c9\u5c0d\u3002\u672c\u6587\u5c07\u70ba\u60a8\u4ecb\u7d39exec()\u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u6280\u5de7\u53ca\u5176\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<p><!--more--><\/p>\n<h2>exec()\u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5<\/h2>\n<p>exec()\u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nexec(object[, globals[, locals]])<br \/>\n&#8220;`<\/p>\n<p>&#8211; **object**\uff1a\u5fc5\u9700\uff0c\u9019\u53ef\u4ee5\u662f\u4e00\u500b\u5b57\u4e32\uff0c\u6216\u662f\u4e00\u500b\u53ef\u57f7\u884c\u7684\u7269\u4ef6\uff08\u5982code\u6216function\uff09\u3002<br \/>\n&#8211; **globals**\uff1a\u53ef\u9078\uff0c\u63d0\u4f9b\u5168\u5c40\u74b0\u5883\u7684\u5b57\u5178\u3002<br \/>\n&#8211; **locals**\uff1a\u53ef\u9078\uff0c\u63d0\u4f9b\u5c40\u90e8\u74b0\u5883\u7684\u5b57\u5178\u3002<\/p>\n<p>\u9019\u500b\u51fd\u6578\u7684\u5f37\u5927\u4e4b\u8655\u5728\u65bc\u5b83\u80fd\u5920\u5728\u6307\u5b9a\u7684\u74b0\u5883\u4e2d\u57f7\u884c\u7a0b\u5f0f\u78bc\uff0c\u9019\u4f7f\u5176\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u9700\u8981\u52d5\u614b\u57f7\u884c\u7a0b\u5f0f\u78bc\u7684\u60c5\u5883\u3002<\/p>\n<h2>\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/h2>\n<p>\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u793a\u7bc4\u4e86\u5982\u4f55\u4f7f\u7528exec()\u51fd\u6578\u57f7\u884c\u4e00\u500b\u7c21\u55ae\u7684\u5b57\u4e32\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = 1<br \/>\ny = 2<br \/>\nexec(&#8216;print(&#8220;x + y =&#8221;, x + y)&#8217;)<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u5c07\u6703\u5f97\u5230\u4ee5\u4e0b\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\nx + y = 3<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0cexec()\u53ef\u4ee5\u52d5\u614b\u57f7\u884c\u7531\u5b57\u4e32\u7d44\u6210\u7684Python\u7a0b\u5f0f\u78bc\uff0c\u9019\u5728\u9700\u8981\u52d5\u614b\u751f\u6210\u7a0b\u5f0f\u78bc\u7684\u61c9\u7528\u5834\u666f\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h2>\u57f7\u884c\u5916\u90e8\u7684Python\u6a94\u6848<\/h2>\n<p>\u9664\u4e86\u57f7\u884c\u5b57\u4e32\uff0cexec()\u51fd\u6578\u9084\u80fd\u5920\u57f7\u884c\u5916\u90e8\u7684Python\u6a94\u6848\u3002\u4e0b\u9762\u7684\u7a0b\u5f0f\u78bc\u793a\u7bc4\u4e86\u5982\u4f55\u4f7f\u7528exec()\u51fd\u6578\u57f7\u884c\u4e00\u500b\u540d\u70ba`test.py`\u7684\u6a94\u6848\uff1a<\/p>\n<p>&#8220;`python<br \/>\nexec(open(&#8216;test.py&#8217;).read())<br \/>\n&#8220;`<\/p>\n<p>\u9019\u884c\u7a0b\u5f0f\u78bc\u6703\u8b80\u53d6`test.py`\u4e2d\u7684\u6240\u6709\u7a0b\u5f0f\u78bc\u4e26\u57f7\u884c\u5b83\u3002\u8acb\u78ba\u4fdd`test.py`\u6a94\u6848\u8207\u7576\u524d\u57f7\u884c\u7684\u8173\u672c\u5728\u540c\u4e00\u76ee\u9304\u4e2d\uff0c\u5426\u5247\u9700\u8981\u63d0\u4f9b\u6b63\u78ba\u7684\u8def\u5f91\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u4f7f\u7528exec()\u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **SyntaxError**\uff1a\u5982\u679c\u50b3\u5165\u7684\u5b57\u4e32\u4e0d\u662f\u6709\u6548\u7684Python\u8a9e\u6cd5\uff0c\u5247\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002<br \/>\n&#8211; **NameError**\uff1a\u7576\u5728\u57f7\u884c\u7684\u5b57\u4e32\u4e2d\u4f7f\u7528\u4e86\u672a\u5b9a\u7fa9\u7684\u8b8a\u6578\u6642\uff0c\u5c07\u5f15\u767c\u6b64\u932f\u8aa4\u3002<\/p>\n<p>\u70ba\u4e86\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\uff0c\u5efa\u8b70\u5728\u57f7\u884c\u5b57\u4e32\u4e4b\u524d\u4f7f\u7528`try&#8230;except`\u584a\u4f86\u6355\u6349\u7570\u5e38\u4e26\u9032\u884c\u8655\u7406\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>exec()\u51fd\u6578\u7684\u61c9\u7528\u4e0d\u50c5\u9650\u65bc\u7c21\u55ae\u7684\u57f7\u884c\u5b57\u4e32\u6216\u6a94\u6848\uff0c\u5b83\u9084\u53ef\u4ee5\u7528\u65bc\uff1a<\/p>\n<p>&#8211; **\u52d5\u614b\u751f\u6210\u51fd\u6578**\uff1a\u6839\u64da\u9700\u6c42\u5728\u904b\u884c\u6642\u5275\u5efa\u548c\u57f7\u884c\u51fd\u6578\u3002<br \/>\n&#8211; **\u4e92\u52d5\u5f0fPython\u74b0\u5883**\uff1a\u5728\u67d0\u4e9b\u61c9\u7528\u4e2d\uff0cexec()\u53ef\u4ee5\u7528\u4f86\u5be6\u73fe\u4e92\u52d5\u5f0f\u7684\u7a0b\u5f0f\u78bc\u57f7\u884c\u74b0\u5883\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3Python\u7684\u9032\u968e\u61c9\u7528\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7[Python\u9032\u968e\u6559\u5b78](https:\/\/vocus.cc)\u4ee5\u7372\u5f97\u66f4\u591a\u5be6\u7528\u8cc7\u8a0a\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86Python\u7684exec()\u51fd\u6578\uff0c\u5305\u62ec\u5176\u57fa\u672c\u8a9e\u6cd5\u3001\u4f7f\u7528\u7bc4\u4f8b\u3001\u5e38\u898b\u932f\u8aa4\u53ca\u5176\u5ef6\u4f38\u61c9\u7528\u3002exec()\u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u7528\u65bc\u57f7\u884c\u52d5\u614b\u751f\u6210\u7684\u7a0b\u5f0f\u78bc\uff0c\u4f46\u9700\u8b39\u614e\u4f7f\u7528\uff0c\u4ee5\u907f\u514d\u5b89\u5168\u98a8\u96aa\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u4f7f\u7528exec()\u51fd\u6578\u662f\u5426\u5b89\u5168\uff1f**<br \/>\nA1: exec()\u51fd\u6578\u5728\u57f7\u884c\u4e0d\u53ef\u4fe1\u7684\u7a0b\u5f0f\u78bc\u6642\u53ef\u80fd\u6703\u5c0e\u81f4\u5b89\u5168\u98a8\u96aa\uff0c\u56e0\u6b64\u61c9\u8b39\u614e\u4f7f\u7528\uff0c\u4e26\u907f\u514d\u57f7\u884c\u4f86\u81ea\u4e0d\u5b89\u5168\u4f86\u6e90\u7684\u4ee3\u78bc\u3002<\/p>\n<p>**Q2: exec()\u51fd\u6578\u80fd\u5426\u7528\u65bc\u57f7\u884c\u591a\u884c\u7a0b\u5f0f\u78bc\uff1f**<br \/>\nA2: \u662f\u7684\uff0cexec()\u51fd\u6578\u53ef\u4ee5\u57f7\u884c\u591a\u884c\u7684\u7a0b\u5f0f\u78bc\uff0c\u53ea\u9700\u5c07\u591a\u884c\u7a0b\u5f0f\u78bc\u653e\u5728\u4e09\u500b\u5f15\u865f\u5167\u7684\u5b57\u4e32\u4e2d\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u5728exec()\u4e2d\u4f7f\u7528\u5c40\u90e8\u8b8a\u6578\uff1f**<br \/>\nA3: \u60a8\u53ef\u4ee5\u901a\u904e`locals`\u53c3\u6578\u5c07\u5c40\u90e8\u8b8a\u6578\u50b3\u905e\u7d66exec()\u51fd\u6578\uff0c\u5728\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u4e2d\u5c31\u80fd\u5920\u8a2a\u554f\u9019\u4e9b\u8b8a\u6578\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>exec()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u57f7\u884c\u5b57\u7b26\u4e32\u4e2d\u7684Python\u4ee3\u78bc\uff0c\u53ef\u4ee5\u7528\u65bc\u57f7\u884c\u52d5\u614b\u7684Python\u4ee3\u78bc\uff0c\u4e26\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39exec()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-2994","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-Mi","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2994","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=2994"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2994\/revisions"}],"predecessor-version":[{"id":12980,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2994\/revisions\/12980"}],"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=2994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}