{"id":2978,"date":"2025-06-02T17:08:29","date_gmt":"2025-06-04T09:15:29","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2978"},"modified":"2025-06-04T17:08:29","modified_gmt":"2025-06-04T09:15:29","slug":"python%e4%b8%ad%e7%9a%84compile%e5%87%bd%e6%95%b8%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e7%94%a8%e7%9a%84%e7%b7%a8%e8%ad%af%e5%99%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84compile%e5%87%bd%e6%95%b8%ef%bc%9a%e7%b0%a1%e5%96%ae%e6%98%93%e7%94%a8%e7%9a%84%e7%b7%a8%e8%ad%af%e5%99%a8\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3 Python \u7684 compile() \u51fd\u6578\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, compile, Python \u6559\u5b78, Python \u51fd\u6578\"><\/p>\n<h1>\u6df1\u5165\u7406\u89e3 Python \u7684 compile() \u51fd\u6578<\/h1>\n<p>Python \u7684 `compile()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u53ef\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u7269\u4ef6\u3002\u9019\u4f7f\u5f97\u6211\u5011\u53ef\u4ee5\u57f7\u884c\u52d5\u614b\u7a0b\u5f0f\u78bc\u6216\u5efa\u7acb\u53ef\u91cd\u8907\u4f7f\u7528\u7684\u7a0b\u5f0f\u78bc\u7269\u4ef6\u3002\u5728\u8a31\u591a\u60c5\u5883\u4e0b\uff0c\u9019\u500b\u51fd\u6578\u90fd\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u7684\u9748\u6d3b\u6027\u548c\u91cd\u7528\u6027\u3002<\/p>\n<p>### `compile()` \u51fd\u6578\u7684\u8a9e\u6cd5<\/p>\n<p>\u4ee5\u4e0b\u662f `compile()` \u51fd\u6578\u7684\u8a9e\u6cd5\uff1a<\/p>\n<pre class=\"brush: python\">\ncompile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1)\n<\/pre>\n<p>&#8211; **source**\uff1a\u8981\u7de8\u8b6f\u7684\u5b57\u4e32\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u5408\u6cd5\u7684 Python \u7a0b\u5f0f\u78bc\u3002<br \/>\n&#8211; **filename**\uff1a\u7528\u4f86\u6307\u5b9a\u7de8\u8b6f\u6642\u7684\u6a94\u6848\u540d\u7a31\uff0c\u9019\u5c0d\u65bc\u932f\u8aa4\u8ffd\u8e64\u975e\u5e38\u91cd\u8981\u3002<br \/>\n&#8211; **mode**\uff1a\u6307\u5b9a\u7de8\u8b6f\u6a21\u5f0f\uff0c\u53ef\u4ee5\u662f `exec`\uff08\u57f7\u884c\u6a21\u7d44\uff09\u3001`eval`\uff08\u8a55\u4f30\u8868\u9054\u5f0f\uff09\u6216 `single`\uff08\u57f7\u884c\u55ae\u4e00\u8a9e\u53e5\uff09\u3002<br \/>\n&#8211; **flags**\uff1a\u7528\u65bc\u63a7\u5236\u7de8\u8b6f\u904e\u7a0b\u7684\u9078\u9805\u3002<br \/>\n&#8211; **dont_inherit**\uff1a\u662f\u5426\u7e7c\u627f\u5168\u57df\u8b8a\u6578\uff0c\u8a2d\u70ba `True` \u5247\u4e0d\u7e7c\u627f\u3002<br \/>\n&#8211; **optimize**\uff1a\u6307\u5b9a\u512a\u5316\u7b49\u7d1a\uff0c\u503c\u53ef\u4ee5\u662f 0\uff08\u4e0d\u512a\u5316\uff09\u30011\uff08\u7c21\u55ae\u512a\u5316\uff09\u30012\uff08\u66f4\u591a\u512a\u5316\uff09\u6216 -1\uff08\u9810\u8a2d\u512a\u5316\uff09\u3002<\/p>\n<p>### \u5be6\u4f5c\u7bc4\u4f8b\uff1a\u4f7f\u7528 `compile()` \u51fd\u6578<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 `compile()` \u51fd\u6578\u7684\u7c21\u55ae\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5c07\u5b57\u4e32\u8f49\u63db\u6210\u53ef\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u7269\u4ef6\uff1a<\/p>\n<pre class=\"brush: python\">\ncode = 'x = 2\\ny = 3\\nprint(\"x + y =\", x + y)'\n\n# \u5c07\u5b57\u4e32\u8f49\u63db\u6210\u53ef\u57f7\u884c\u7684\u7a0b\u5f0f\u78bc\u7269\u4ef6\ncode_obj = compile(code, '', 'exec')\n\n# \u57f7\u884c\u7a0b\u5f0f\u78bc\u7269\u4ef6\nexec(code_obj)\n<\/pre>\n<p>\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\nx + y = 5\n<\/pre>\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u57f7\u884c\u7a0b\u5f0f\u78bc\u7269\u4ef6\u5f8c\uff0c\u6211\u5011\u5f97\u5230\u4e86\u6b63\u78ba\u7684\u7d50\u679c\uff0c\u9019\u986f\u793a\u4e86 `compile()` \u51fd\u6578\u7684\u5be6\u7528\u6027\u3002<\/p>\n<p>### `compile()` \u51fd\u6578\u7684\u53c3\u6578\u8a73\u7d30\u4ecb\u7d39<\/p>\n<p>`compile()` \u51fd\u6578\u7684\u4e94\u500b\u53c3\u6578\u5404\u6709\u5176\u7279\u5b9a\u7528\u9014\uff0c\u4e0b\u9762\u5c07\u91dd\u5c0d\u6bcf\u4e00\u500b\u53c3\u6578\u9032\u884c\u66f4\u8a73\u7d30\u7684\u8aaa\u660e\uff1a<\/p>\n<p>&#8211; **source**\uff1a\u53ef\u4ee5\u662f\u5b57\u4e32\u6216\u5b57\u4e32\u5e8f\u5217\uff08\u4f8b\u5982 list \u6216 tuple\uff09\u3002<br \/>\n&#8211; **filename**\uff1a\u6307\u660e\u4f86\u6e90\u6a94\u6848\u540d\u7a31\uff0c\u9019\u5c0d\u65bc\u932f\u8aa4\u8a0a\u606f\u7684\u56de\u6eaf\u975e\u5e38\u91cd\u8981\u3002<br \/>\n&#8211; **mode**\uff1a<br \/>\n  &#8211; `exec`\uff1a\u7528\u65bc\u57f7\u884c\u591a\u884c\u7a0b\u5f0f\u78bc\u3002<br \/>\n  &#8211; `eval`\uff1a\u7528\u65bc\u8a55\u4f30\u55ae\u4e00\u8868\u9054\u5f0f\u3002<br \/>\n  &#8211; `single`\uff1a\u7528\u65bc\u57f7\u884c\u55ae\u4e00\u8a9e\u53e5\u3002<br \/>\n&#8211; **flags**\uff1a\u53ef\u4ee5\u662f\u591a\u500b\u9078\u9805\u7684\u7d44\u5408\uff0c\u4f8b\u5982\uff1a<br \/>\n  &#8211; `ast.PyCF_ONLY_AST`\uff1a\u53ea\u8fd4\u56de\u62bd\u8c61\u8a9e\u6cd5\u6a39\uff0c\u4e0d\u7de8\u8b6f\u7a0b\u5f0f\u78bc\u3002<br \/>\n  &#8211; `ast.PyCF_ALLOW_TOP_LEVEL_AWAIT`\uff1a\u5141\u8a31\u9802\u5c64\u7684 `await` \u8a9e\u53e5\u3002<br \/>\n&#8211; **dont_inherit**\uff1a\u82e5\u8a2d\u70ba `True`\uff0c\u5247\u4e0d\u6703\u7e7c\u627f\u5168\u57df\u8b8a\u6578\uff1b\u82e5\u8a2d\u70ba `False`\uff0c\u5247\u6703\u7e7c\u627f\u3002<br \/>\n&#8211; **optimize**\uff1a\u53ef\u4ee5\u7528\u4f86\u8a2d\u5b9a\u512a\u5316\u7b49\u7d1a\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664\u8207\u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u5728\u4f7f\u7528 `compile()` \u51fd\u6578\u6642\uff0c\u5982\u679c\u9047\u5230\u7de8\u8b6f\u932f\u8aa4\uff0c\u901a\u5e38\u6703\u5f15\u767c `SyntaxError`\u3002\u5efa\u8b70\u6aa2\u67e5 `source` \u5b57\u4e32\u7684\u8a9e\u6cd5\uff0c\u78ba\u4fdd\u5176\u70ba\u6709\u6548\u7684 Python \u7a0b\u5f0f\u78bc\u3002<\/p>\n<p>\u6b64\u5916\uff0c`compile()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u65bc\u52d5\u614b\u751f\u6210\u7a0b\u5f0f\u78bc\u7684\u61c9\u7528\u5834\u666f\uff0c\u4f8b\u5982\u5728 Web \u61c9\u7528\u4e2d\u52d5\u614b\u751f\u6210\u67e5\u8a62\u6216\u5728\u6e2c\u8a66\u4e2d\u57f7\u884c\u52d5\u614b\u751f\u6210\u7684\u6e2c\u8a66\u6848\u4f8b\u3002\u9019\u7a2e\u9748\u6d3b\u6027\u4f7f\u5f97 Python \u5728\u8a31\u591a\u61c9\u7528\u9818\u57df\u4e2d\u90fd\u8868\u73fe\u5f97\u5c24\u70ba\u51fa\u8272\u3002<\/p>\n<p>\u5982\u679c\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u52d5\u614b\u57f7\u884c\u8207\u7de8\u8b6f\u5de5\u5177\uff0c\u63a8\u85a6\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/637c2a3ef9a8f20001bdf1c1) \u9032\u4e00\u6b65\u63a2\u7d22\u3002<\/p>\n<p>### \u7d50\u8ad6<\/p>\n<p>Python \u7684 `compile()` \u51fd\u6578\u662f\u57f7\u884c\u52d5\u614b\u7a0b\u5f0f\u78bc\u548c\u5efa\u7acb\u53ef\u91cd\u8907\u4f7f\u7528\u7684\u7a0b\u5f0f\u78bc\u7269\u4ef6\u7684\u5f37\u5927\u5de5\u5177\u3002\u638c\u63e1\u9019\u500b\u51fd\u6578\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u4f60\u7684\u7de8\u7a0b\u80fd\u529b\u548c\u9748\u6d3b\u6027\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `compile()` \u51fd\u6578\u7684 `mode` \u53c3\u6578\u6709\u4f55\u4e0d\u540c\uff1f**<br \/>\nA1: `exec` \u7528\u65bc\u57f7\u884c\u591a\u884c\u7a0b\u5f0f\u78bc\uff0c`eval` \u7528\u65bc\u8a55\u4f30\u55ae\u4e00\u8868\u9054\u5f0f\uff0c`single` \u5247\u57f7\u884c\u55ae\u4e00\u8a9e\u53e5\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 `compile()` \u51fd\u6578\u7684\u7de8\u8b6f\u932f\u8aa4\uff1f**<br \/>\nA2: \u61c9\u6aa2\u67e5 `source` \u5b57\u4e32\u7684\u8a9e\u6cd5\uff0c\u78ba\u4fdd\u5176\u70ba\u6709\u6548\u7684 Python \u7a0b\u5f0f\u78bc\uff0c\u4e26\u5728\u5fc5\u8981\u6642\u4f7f\u7528\u932f\u8aa4\u8655\u7406\u6a5f\u5236\u6355\u7372 `SyntaxError`\u3002<\/p>\n<p>**Q3: `compile()` \u662f\u5426\u80fd\u7528\u65bc\u57f7\u884c\u5916\u90e8\u6a94\u6848\u4e2d\u7684\u7a0b\u5f0f\u78bc\uff1f**<br \/>\nA3: `compile()` \u672c\u8eab\u7121\u6cd5\u76f4\u63a5\u57f7\u884c\u6a94\u6848\uff0c\u4f46\u53ef\u4ee5\u8b80\u53d6\u6a94\u6848\u5167\u5bb9\u4f5c\u70ba `source` \u53c3\u6578\u4f86\u7de8\u8b6f\u548c\u57f7\u884c\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684compile()\u51fd\u6578\u53ef\u4ee5\u5c07\u5b57\u4e32\u7de8\u8b6f\u6210Python\u4ee3\u78bc\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u57f7\u884c\u7a0b\u5f0f\u78bc\uff0c\u63d0\u9ad8\u7a0b\u5f0f\u57f7\u884c\u6548\u7387\u3002\u672c\u6587\u5c07\u4ecb\u7d39compile()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u5b83\u4f86\u63d0\u9ad8\u7a0b\u5f0f\u57f7\u884c\u6548\u7387\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-2978","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-M2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2978","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=2978"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2978\/revisions"}],"predecessor-version":[{"id":12987,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2978\/revisions\/12987"}],"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=2978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}