{"id":973455,"date":"2024-12-27T05:57:02","date_gmt":"2024-12-26T21:57:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/973455.html"},"modified":"2024-12-27T05:57:04","modified_gmt":"2024-12-26T21:57:04","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b%e5%ad%97%e8%8a%82%e7%a0%81python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/973455.html","title":{"rendered":"\u5982\u4f55\u67e5\u770b\u5b57\u8282\u7801python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195633\/1a784b43-91c7-4c24-825a-af413a51c885.webp\" alt=\"\u5982\u4f55\u67e5\u770b\u5b57\u8282\u7801python\" \/><\/p>\n<p><p> <strong>\u67e5\u770bPython\u5b57\u8282\u7801\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>dis<\/code>\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5de5\u5177\u5982<code>py_compile<\/code>\u548c<code>marshal<\/code>\u6a21\u5757\u3001\u4ee5\u53ca\u4f7f\u7528Python\u7f16\u8bd1\u5668\u7684\u547d\u4ee4\u884c\u53c2\u6570\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u67e5\u770b\u548c\u5206\u6790Python\u4ee3\u7801\u7684\u5b57\u8282\u7801\uff0c\u63d0\u9ad8\u5bf9\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u7684\u7406\u89e3\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e\u6a21\u5757<code>dis<\/code>\uff0c\u5b83\u53ef\u4ee5\u5c06Python\u6e90\u4ee3\u7801\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u53cd\u6c47\u7f16\u6210\u4eba\u7c7b\u53ef\u8bfb\u7684\u683c\u5f0f\uff0c\u4ece\u800c\u5e2e\u52a9\u5f00\u53d1\u8005\u7406\u89e3Python\u7684\u6267\u884c\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p><code>dis<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u51fd\u6570\u8c03\u7528\uff0c\u5c06Python\u51fd\u6570\u7684\u5b57\u8282\u7801\u8f93\u51fa\u5728\u7ec8\u7aef\u6216\u65e5\u5fd7\u4e2d\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u3001\u4f18\u5316\u548c\u6559\u5b66\u90fd\u6709\u5f88\u5927\u7684\u5e2e\u52a9\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>dis<\/code>\u6a21\u5757\u67e5\u770b\u5b57\u8282\u7801\u4ee5\u53ca\u5176\u4ed6\u67e5\u770b\u5b57\u8282\u7801\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>dis<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>dis<\/code> \u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u53cd\u6c47\u7f16Python\u5b57\u8282\u7801\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5206\u6790Python\u7684\u5b57\u8282\u7801\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528 <code>dis<\/code> \u6a21\u5757\u67e5\u770b\u51fd\u6570\u5b57\u8282\u7801<\/h4>\n<\/p>\n<p><p>\u8981\u67e5\u770bPython\u51fd\u6570\u7684\u5b57\u8282\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dis<\/code>\u6a21\u5757\u7684<code>dis()<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dis<\/p>\n<p>def example_function(x):<\/p>\n<p>    return x + 1<\/p>\n<p>dis.dis(example_function)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>example_function<\/code>\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u51fd\u6570\u3002\u4f7f\u7528<code>dis.dis(example_function)<\/code>\u53ef\u4ee5\u6253\u5370\u51fa\u8be5\u51fd\u6570\u7684\u5b57\u8282\u7801\u3002\u8f93\u51fa\u7ed3\u679c\u5c06\u5305\u542b\u64cd\u4f5c\u7801\u53ca\u5176\u5bf9\u5e94\u7684\u53c2\u6570\uff0c\u663e\u793a\u4e86Python\u89e3\u91ca\u5668\u5982\u4f55\u6267\u884c\u8be5\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5206\u6790\u5b57\u8282\u7801\u8f93\u51fa<\/h4>\n<\/p>\n<p><p>\u5b57\u8282\u7801\u8f93\u51fa\u901a\u5e38\u7531\u4e00\u7cfb\u5217\u7684\u6307\u4ee4\u7ec4\u6210\uff0c\u6bcf\u4e00\u884c\u663e\u793a\u4e00\u4e2a\u64cd\u4f5c\u7801\u53ca\u5176\u53ef\u9009\u53c2\u6570\u3002\u64cd\u4f5c\u7801\u901a\u5e38\u662f\u5b57\u6bcd\u7f29\u5199\uff0c\u5982<code>LOAD_FAST<\/code>\u3001<code>RETURN_VALUE<\/code>\u7b49\uff0c\u8fd9\u4e9b\u6307\u4ee4\u89e3\u91ca\u5668\u7528\u4e8e\u64cd\u4f5c\u6808\u4e0a\u7684\u503c\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>LOAD_FAST<\/strong>\uff1a\u4ece\u5c40\u90e8\u53d8\u91cf\u4e2d\u52a0\u8f7d\u4e00\u4e2a\u503c\u5230\u6808\u9876\u3002<\/li>\n<li><strong>RETURN_VALUE<\/strong>\uff1a\u4ece\u6808\u9876\u5f39\u51fa\u4e00\u4e2a\u503c\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u5206\u6790\u8fd9\u4e9b\u6307\u4ee4\uff0c\u53ef\u4ee5\u4e86\u89e3\u51fd\u6570\u7684\u6267\u884c\u6d41\u7a0b\u4ee5\u53ca\u4f18\u5316\u7684\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>py_compile<\/code> \u548c <code>marshal<\/code> \u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>dis<\/code>\u6a21\u5757\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65b9\u6cd5\u6765\u67e5\u770b\u5b57\u8282\u7801\uff0c\u5982<code>py_compile<\/code>\u548c<code>marshal<\/code>\u6a21\u5757\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528 <code>py_compile<\/code> \u7f16\u8bd1Python\u4ee3\u7801<\/h4>\n<\/p>\n<p><p><code>py_compile<\/code>\u6a21\u5757\u7528\u4e8e\u5c06Python\u6e90\u6587\u4ef6\u7f16\u8bd1\u4e3a\u5b57\u8282\u7801\u6587\u4ef6\uff08<code>.pyc<\/code>\u6587\u4ef6\uff09\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528<code>py_compile<\/code>\u7f16\u8bd1Python\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import py_compile<\/p>\n<p>py_compile.compile(&#39;example.py&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u5c06\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\u751f\u6210\u4e00\u4e2a<code>example.pyc<\/code>\u6587\u4ef6\uff0c\u8be5\u6587\u4ef6\u5305\u542b\u7f16\u8bd1\u540e\u7684\u5b57\u8282\u7801\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u8bfb\u53d6 <code>.pyc<\/code> \u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>marshal<\/code>\u6a21\u5757\u53ef\u4ee5\u8bfb\u53d6<code>.pyc<\/code>\u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import marshal<\/p>\n<p>with open(&#39;__pycache__\/example.cpython-38.pyc&#39;, &#39;rb&#39;) as f:<\/p>\n<p>    f.read(16)  # \u8df3\u8fc7\u6587\u4ef6\u5934<\/p>\n<p>    code = marshal.load(f)<\/p>\n<p>import dis<\/p>\n<p>dis.dis(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u67e5\u770b<code>.pyc<\/code>\u6587\u4ef6\u4e2d\u7684\u5b57\u8282\u7801\uff0c\u8fdb\u4e00\u6b65\u4e86\u89e3Python\u4ee3\u7801\u7684\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u547d\u4ee4\u884c\u53c2\u6570\u67e5\u770b\u5b57\u8282\u7801<\/h3>\n<\/p>\n<p><p>Python\u89e3\u91ca\u5668\u63d0\u4f9b\u4e86\u4e00\u4e9b\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u67e5\u770b\u5b57\u8282\u7801\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528 <code>-m dis<\/code> \u53c2\u6570<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>-m dis<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u547d\u4ee4\u884c\u4e2d\u67e5\u770bPython\u6587\u4ef6\u7684\u5b57\u8282\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m dis example.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u547d\u4ee4\u5c06\u6253\u5370\u51fa<code>example.py<\/code>\u4e2d\u6240\u6709\u51fd\u6570\u7684\u5b57\u8282\u7801\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u811a\u672c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u4fbf\u6377\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b57\u8282\u7801\u4f18\u5316\u4e0e\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u67e5\u770b\u5b57\u8282\u7801\u4e0d\u4ec5\u6709\u52a9\u4e8e\u7406\u89e3Python\u4ee3\u7801\u7684\u6267\u884c\uff0c\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u7406\u89e3\u5b57\u8282\u7801\u6307\u4ee4\u7684\u610f\u4e49<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u67e5\u770b\u5b57\u8282\u7801\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8bc6\u522b\u51fa\u53ef\u80fd\u7684\u6027\u80fd\u74f6\u9888\u3002\u4f8b\u5982\uff0c\u9891\u7e41\u7684\u53d8\u91cf\u52a0\u8f7d\u548c\u5b58\u50a8\u53ef\u80fd\u610f\u5473\u7740\u4ee3\u7801\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u53d8\u91cf\u5206\u914d\u6765\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528\u5b57\u8282\u7801\u8fdb\u884c\u6027\u80fd\u8c03\u4f18<\/h4>\n<\/p>\n<p><p>\u4e86\u89e3\u5b57\u8282\u7801\u540e\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5c1d\u8bd5\u901a\u8fc7\u4ee3\u7801\u91cd\u6784\u6765\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u6307\u4ee4\u3002\u4f8b\u5982\uff0c\u5c06\u91cd\u590d\u7684\u8ba1\u7b97\u7ed3\u679c\u5b58\u50a8\u5728\u53d8\u91cf\u4e2d\uff0c\u907f\u514d\u591a\u6b21\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u67e5\u770bPython\u5b57\u8282\u7801\u662f\u7406\u89e3Python\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\u7684\u91cd\u8981\u4e00\u6b65\u3002\u901a\u8fc7\u4f7f\u7528<code>dis<\/code>\u6a21\u5757\u3001<code>py_compile<\/code>\u6a21\u5757\u548c\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u5730\u67e5\u770b\u548c\u5206\u6790\u5b57\u8282\u7801\u3002\u901a\u8fc7\u5bf9\u5b57\u8282\u7801\u7684\u5206\u6790\uff0c\u53ef\u4ee5\u8bc6\u522b\u4ee3\u7801\u4e2d\u7684\u6027\u80fd\u74f6\u9888\uff0c\u5e76\u8fdb\u884c\u4f18\u5316\u3002\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u6027\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u52a0\u6df1\u5bf9Python\u5185\u90e8\u673a\u5236\u7684\u7406\u89e3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770bPython\u7a0b\u5e8f\u7684\u5b57\u8282\u7801\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u7a0b\u5e8f\u7684\u5b57\u8282\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dis<\/code>\u6a21\u5757\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>dis<\/code>\u6a21\u5757\uff0c\u5e76\u8c03\u7528<code>dis.dis()<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165\u60a8\u60f3\u8981\u67e5\u770b\u7684\u51fd\u6570\u6216\u4ee3\u7801\u5757\u3002\u8fd9\u5c06\u6253\u5370\u51fa\u76f8\u5e94\u7684\u5b57\u8282\u7801\u6307\u4ee4\uff0c\u5e2e\u52a9\u60a8\u7406\u89e3Python\u5982\u4f55\u6267\u884c\u8fd9\u4e9b\u6307\u4ee4\u3002<\/p>\n<p><strong>\u5b57\u8282\u7801\u5728Python\u4e2d\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5b57\u8282\u7801\u662fPython\u6e90\u4ee3\u7801\u7f16\u8bd1\u540e\u751f\u6210\u7684\u4e00\u79cd\u4e2d\u95f4\u8868\u793a\u5f62\u5f0f\uff0c\u4e3b\u8981\u7528\u4e8e\u63d0\u9ad8\u6267\u884c\u6548\u7387\u3002\u5b83\u662fPython\u865a\u62df\u673a\uff08PVM\uff09\u53ef\u4ee5\u7406\u89e3\u7684\u4ee3\u7801\uff0c\u5141\u8bb8Python\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u4ee5\u76f8\u540c\u7684\u65b9\u5f0f\u8fd0\u884c\u3002\u8fd9\u79cd\u673a\u5236\u4f7f\u5f97Python\u5177\u5907\u4e86\u8de8\u5e73\u53f0\u7684\u4f18\u52bf\u3002<\/p>\n<p><strong>\u67e5\u770b\u5b57\u8282\u7801\u65f6\uff0c\u5e38\u89c1\u7684\u6307\u4ee4\u548c\u5b83\u4eec\u7684\u542b\u4e49\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u67e5\u770b\u5b57\u8282\u7801\u65f6\uff0c\u60a8\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u6307\u4ee4\uff0c\u4f8b\u5982<code>LOAD_FAST<\/code>\u3001<code>STORE_FAST<\/code>\u3001<code>CALL_FUNCTION<\/code>\u7b49\u3002<code>LOAD_FAST<\/code>\u7528\u4e8e\u52a0\u8f7d\u5c40\u90e8\u53d8\u91cf\uff0c<code>STORE_FAST<\/code>\u7528\u4e8e\u5b58\u50a8\u5c40\u90e8\u53d8\u91cf\uff0c\u800c<code>CALL_FUNCTION<\/code>\u5219\u7528\u4e8e\u8c03\u7528\u51fd\u6570\u3002\u8fd9\u4e9b\u6307\u4ee4\u7684\u7ec4\u5408\u51b3\u5b9a\u4e86\u4ee3\u7801\u7684\u6267\u884c\u6d41\u7a0b\u548c\u6027\u80fd\u8868\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u67e5\u770bPython\u5b57\u8282\u7801\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684dis\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5de5\u5177\u5982py_compile\u548cmarshal [&hellip;]","protected":false},"author":3,"featured_media":973459,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973455"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=973455"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973455\/revisions"}],"predecessor-version":[{"id":973462,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973455\/revisions\/973462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/973459"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=973455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=973455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=973455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}