{"id":1168269,"date":"2025-01-15T15:55:56","date_gmt":"2025-01-15T07:55:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1168269.html"},"modified":"2025-01-15T15:55:58","modified_gmt":"2025-01-15T07:55:58","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e5%8c%85%e7%9a%84%e6%ba%90%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1168269.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u5305\u7684\u6e90\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065548\/e504344d-ced8-4353-bc22-35e4930a5457.webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u5305\u7684\u6e90\u7801\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u67e5\u770bPython\u5305\u7684\u6e90\u7801<\/strong>\uff1a<strong>\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>source<\/code>\u547d\u4ee4\u3001\u76f4\u63a5\u67e5\u770b\u5b89\u88c5\u76ee\u5f55\u4e2d\u7684\u6e90\u7801\u6587\u4ef6<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u662f\u4e00\u79cd\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7<code>inspect<\/code>\u6a21\u5757\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u52a8\u6001\u83b7\u53d6\u6a21\u5757\u3001\u7c7b\u548c\u51fd\u6570\u7684\u6e90\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u67e5\u770b\u51fd\u6570\u6216\u7c7b\u7684\u6e90\u7801\u3002<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7684\u5404\u79cd\u4fe1\u606f\uff0c\u5305\u62ec\u5176\u6e90\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import some_module  # \u5047\u8bbe\u6211\u4eec\u8981\u67e5\u770bsome_module\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\u7684\u6e90\u7801<\/p>\n<h2><strong>\u83b7\u53d6\u51fd\u6570\u5bf9\u8c61<\/strong><\/h2>\n<p>func = some_module.some_function<\/p>\n<h2><strong>\u83b7\u53d6\u51fd\u6570\u7684\u6e90\u7801<\/strong><\/h2>\n<p>source_code = inspect.getsource(func)<\/p>\n<h2><strong>\u6253\u5370\u6e90\u7801<\/strong><\/h2>\n<p>print(source_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>inspect.getsource()<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u4efb\u4f55\u53ef\u8c03\u7528\u5bf9\u8c61\uff08\u5982\u51fd\u6570\u3001\u65b9\u6cd5\uff09\u7684\u6e90\u7801\u3002\u5b83\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6e90\u7801\u7684\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\u51fa\u6765\u6216\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>source<\/code>\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\uff0c\u5982IPython\u6216Jupyter Notebook\uff0c\u53ef\u4ee5\u4f7f\u7528<code>%source<\/code>\u9b54\u6cd5\u547d\u4ee4\u6765\u67e5\u770b\u51fd\u6570\u6216\u7c7b\u7684\u6e90\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u65b9\u4fbf\uff0c\u5c24\u5176\u662f\u5728\u63a2\u7d22\u4ee3\u7801\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">%source some_module.some_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u65e0\u9700\u7f16\u5199\u4efb\u4f55\u989d\u5916\u4ee3\u7801\uff0c\u53ea\u9700\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u8f93\u5165\u547d\u4ee4\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u76f4\u63a5\u67e5\u770b\u5b89\u88c5\u76ee\u5f55\u4e2d\u7684\u6e90\u7801\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u76f4\u63a5\u67e5\u770b\u5b89\u88c5\u76ee\u5f55\u4e2d\u7684\u6e90\u7801\u6587\u4ef6\u4e5f\u662f\u4e00\u79cd\u7b80\u5355\u6709\u6548\u7684\u65b9\u6cd5\u3002\u901a\u5e38\uff0cPython\u5305\u7684\u6e90\u7801\u5b89\u88c5\u5728\u7cfb\u7edf\u7684<code>site-packages<\/code>\u76ee\u5f55\u4e2d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u627e\u5230\u5e76\u67e5\u770b\u6e90\u7801\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u9996\u5148\uff0c\u627e\u5230Python\u5305\u7684\u5b89\u88c5\u76ee\u5f55\u3002\u53ef\u4ee5\u4f7f\u7528<code>pip show<\/code>\u547d\u4ee4\u6765\u83b7\u53d6\u5305\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u5b89\u88c5\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip show some_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u8f93\u51fa\u4e00\u4e9b\u5173\u4e8e<code>some_module<\/code>\u5305\u7684\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u62ec<code>Location<\/code>\u5b57\u6bb5\uff0c\u6307\u793a\u5305\u7684\u5b89\u88c5\u8def\u5f84\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u7136\u540e\uff0c\u5bfc\u822a\u5230\u8be5\u8def\u5f84\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u7ba1\u7406\u5668\u6216\u547d\u4ee4\u884c\u5de5\u5177\u6765\u6d4f\u89c8\u8be5\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u6700\u540e\uff0c\u627e\u5230\u76f8\u5e94\u7684\u6e90\u7801\u6587\u4ef6\u5e76\u6253\u5f00\u67e5\u770b\u3002\u901a\u5e38\uff0c\u6e90\u7801\u6587\u4ef6\u662f\u4ee5<code>.py<\/code>\u4e3a\u6269\u5c55\u540d\u7684\u7eaf\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6587\u672c\u7f16\u8f91\u5668\u6765\u67e5\u770b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u67e5\u770bPython\u5305\u7684\u6e90\u7801\uff1a<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528IDE<\/h4>\n<\/p>\n<p><p>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u63d0\u4f9b\u4e86\u67e5\u770b\u6e90\u7801\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0cPyCharm\u548cVSCode\u90fd\u53ef\u4ee5\u901a\u8fc7\u5feb\u6377\u952e\u6216\u83dc\u5355\u9009\u9879\u76f4\u63a5\u8df3\u8f6c\u5230\u51fd\u6570\u6216\u7c7b\u7684\u5b9a\u4e49\uff0c\u4ece\u800c\u67e5\u770b\u6e90\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5728\u7ebf\u4ee3\u7801\u5e93<\/h4>\n<\/p>\n<p><p>\u8bb8\u591a\u5f00\u6e90Python\u5305\u7684\u6e90\u7801\u6258\u7ba1\u5728GitHub\u7b49\u4ee3\u7801\u6258\u7ba1\u5e73\u53f0\u4e0a\u3002\u60a8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e9b\u5e73\u53f0\u4e0a\u7684\u9879\u76ee\u9875\u9762\uff0c\u6d4f\u89c8\u6e90\u7801\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u8fd9\u4e9b\u5e73\u53f0\u901a\u5e38\u8fd8\u63d0\u4f9b\u4e86\u641c\u7d22\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5feb\u901f\u627e\u5230\u7279\u5b9a\u7684\u51fd\u6570\u6216\u7c7b\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528\u6587\u6863\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u4e00\u4e9b\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u5982Sphinx\uff0c\u53ef\u4ee5\u751f\u6210\u5305\u542b\u6e90\u7801\u7684\u6587\u6863\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u4f1a\u5728\u751f\u6210\u7684\u6587\u6863\u4e2d\u5305\u542b\u94fe\u63a5\uff0c\u70b9\u51fb\u8fd9\u4e9b\u94fe\u63a5\u53ef\u4ee5\u76f4\u63a5\u67e5\u770b\u76f8\u5e94\u7684\u6e90\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u67e5\u770b\u6807\u51c6\u5e93\u6e90\u7801<\/h3>\n<\/p>\n<p><p>Python\u6807\u51c6\u5e93\u7684\u6e90\u7801\u901a\u5e38\u5305\u542b\u5728Python\u5b89\u88c5\u76ee\u5f55\u4e2d\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u67e5\u770b\u6807\u51c6\u5e93\u7684\u6e90\u7801\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p>\u627e\u5230Python\u7684\u5b89\u88c5\u76ee\u5f55\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u64cd\u4f5c\u7cfb\u7edf\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u627e\u5230Python\u7684\u5b89\u88c5\u8def\u5f84\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">which python<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u8f93\u51faPython\u89e3\u91ca\u5668\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u53ef\u80fd\u662f<code>\/usr\/local\/bin\/python<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5bfc\u822a\u5230\u5b89\u88c5\u76ee\u5f55\u3002\u4f8b\u5982\uff0c\u5982\u679cPython\u5b89\u88c5\u5728<code>\/usr\/local<\/code>\u76ee\u5f55\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5bfc\u822a\u5230\u6807\u51c6\u5e93\u76ee\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">cd \/usr\/local\/lib\/python3.9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u6839\u636e\u60a8\u7684Python\u7248\u672c\u8c03\u6574\u8def\u5f84\u4e2d\u7684\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u5728\u6807\u51c6\u5e93\u76ee\u5f55\u4e2d\uff0c\u60a8\u53ef\u4ee5\u627e\u5230\u6240\u6709\u6807\u51c6\u5e93\u6a21\u5757\u7684\u6e90\u7801\u6587\u4ef6\u3002\u4f8b\u5982\uff0c<code>os<\/code>\u6a21\u5757\u7684\u6e90\u7801\u6587\u4ef6\u662f<code>os.py<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u67e5\u770b\u51fd\u6570\u7684\u6e90\u7801\uff0c\u5e76\u5c06\u5176\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import some_module  # \u5047\u8bbe\u6211\u4eec\u8981\u67e5\u770bsome_module\u6a21\u5757\u4e2d\u7684\u67d0\u4e2a\u51fd\u6570\u7684\u6e90\u7801<\/p>\n<h2><strong>\u83b7\u53d6\u51fd\u6570\u5bf9\u8c61<\/strong><\/h2>\n<p>func = some_module.some_function<\/p>\n<h2><strong>\u83b7\u53d6\u51fd\u6570\u7684\u6e90\u7801<\/strong><\/h2>\n<p>source_code = inspect.getsource(func)<\/p>\n<h2><strong>\u6253\u5370\u6e90\u7801<\/strong><\/h2>\n<p>print(source_code)<\/p>\n<h2><strong>\u5c06\u6e90\u7801\u4fdd\u5b58\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;some_function_source.py&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(source_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u9996\u5148\u4f7f\u7528<code>inspect.getsource()<\/code>\u51fd\u6570\u83b7\u53d6\u51fd\u6570\u7684\u6e90\u7801\uff0c\u7136\u540e\u5c06\u6e90\u7801\u6253\u5370\u5230\u63a7\u5236\u53f0\uff0c\u6700\u540e\u5c06\u6e90\u7801\u4fdd\u5b58\u5230\u4e00\u4e2a\u540d\u4e3a<code>some_function_source.py<\/code>\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u67e5\u770bC\u6269\u5c55\u6a21\u5757\u6e90\u7801<\/h3>\n<\/p>\n<p><p>\u67d0\u4e9bPython\u5305\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u7279\u522b\u662f\u4e00\u4e9b\u6027\u80fd\u5173\u952e\u7684\u5305\uff0c\u5982<code>numpy<\/code>\u548c<code>pandas<\/code>\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u5305\uff0c\u60a8\u53ef\u4ee5\u67e5\u770b\u5176C\u6e90\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u627e\u5230\u6e90\u7801\u4ed3\u5e93<\/strong>\uff1a\u5927\u591a\u6570\u5f00\u6e90\u5305\u7684\u6e90\u7801\u6258\u7ba1\u5728GitHub\u7b49\u5e73\u53f0\u4e0a\u3002\u60a8\u53ef\u4ee5\u8bbf\u95ee\u9879\u76ee\u7684GitHub\u9875\u9762\uff0c\u6d4f\u89c8C\u6e90\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u67e5\u770bC\u6e90\u7801\u6587\u4ef6<\/strong>\uff1aC\u6e90\u7801\u6587\u4ef6\u901a\u5e38\u4ee5<code>.c<\/code>\u6216<code>.h<\/code>\u4e3a\u6269\u5c55\u540d\u3002\u60a8\u53ef\u4ee5\u5728\u9879\u76ee\u7684<code>src<\/code>\u6216<code>include<\/code>\u76ee\u5f55\u4e2d\u627e\u5230\u8fd9\u4e9b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9605\u8bfb\u6587\u6863<\/strong>\uff1a\u8bb8\u591a\u9879\u76ee\u4f1a\u63d0\u4f9b\u6587\u6863\uff0c\u89e3\u91caC\u6269\u5c55\u6a21\u5757\u7684\u5b9e\u73b0\u7ec6\u8282\u3002\u9605\u8bfb\u8fd9\u4e9b\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3C\u6e90\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u81ea\u52a8\u5316\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u8fd8\u6709\u4e00\u4e9b\u81ea\u52a8\u5316\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u65b9\u4fbf\u5730\u67e5\u770b\u6e90\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1. <strong>pydoc<\/strong><\/h4>\n<\/p>\n<p><p><code>pydoc<\/code>\u662fPython\u5185\u7f6e\u7684\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u67e5\u770b\u6a21\u5757\u3001\u7c7b\u548c\u51fd\u6570\u7684\u6587\u6863\u548c\u6e90\u7801\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u67d0\u4e2a\u6a21\u5757\u7684\u6587\u6863\u548c\u6e90\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pydoc some_module<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4f1a\u5728\u7ec8\u7aef\u4e2d\u8f93\u51fa\u6a21\u5757\u7684\u6587\u6863\u548c\u6e90\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. <strong>IPython<\/strong><\/h4>\n<\/p>\n<p><p>IPython\u662f\u4e00\u4e2a\u589e\u5f3a\u7248\u7684Python\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u6709\u7528\u7684\u529f\u80fd\uff0c\u5305\u62ec\u67e5\u770b\u6e90\u7801\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u67d0\u4e2a\u51fd\u6570\u7684\u6e90\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import some_module<\/p>\n<p>some_module.some_function??<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528\u4e24\u4e2a\u95ee\u53f7\uff08<code>??<\/code>\uff09\u53ef\u4ee5\u67e5\u770b\u51fd\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u6e90\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u67e5\u770b\u79c1\u6709\u65b9\u6cd5\u548c\u7c7b\u7684\u6e90\u7801<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u67e5\u770b\u5305\u4e2d\u5b9a\u4e49\u7684\u79c1\u6709\u65b9\u6cd5\u6216\u7c7b\u7684\u6e90\u7801\u3002\u79c1\u6709\u65b9\u6cd5\u548c\u7c7b\u901a\u5e38\u4ee5\u5355\u4e0b\u5212\u7ebf\u6216\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u3002\u867d\u7136\u8fd9\u4e9b\u65b9\u6cd5\u548c\u7c7b\u4e0d\u662f\u516c\u5f00API\u7684\u4e00\u90e8\u5206\uff0c\u4f46\u60a8\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u67e5\u770b\u5b83\u4eec\u7684\u6e90\u7801\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u67e5\u770b\u79c1\u6709\u65b9\u6cd5\u7684\u6e90\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import some_module<\/p>\n<h2><strong>\u83b7\u53d6\u79c1\u6709\u65b9\u6cd5\u5bf9\u8c61<\/strong><\/h2>\n<p>private_method = some_module._private_method<\/p>\n<h2><strong>\u83b7\u53d6\u79c1\u6709\u65b9\u6cd5\u7684\u6e90\u7801<\/strong><\/h2>\n<p>source_code = inspect.getsource(private_method)<\/p>\n<h2><strong>\u6253\u5370\u6e90\u7801<\/strong><\/h2>\n<p>print(source_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u67e5\u770b\u7b2c\u4e09\u65b9\u5305\u6e90\u7801\u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u67e5\u770b\u7b2c\u4e09\u65b9\u5305\u7684\u6e90\u7801\u65f6\uff0c\u6709\u4e00\u4e9b\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5c0a\u91cd\u7248\u6743<\/strong>\uff1a\u67e5\u770b\u548c\u5b66\u4e60\u5f00\u6e90\u8f6f\u4ef6\u7684\u6e90\u7801\u65f6\uff0c\u5e94\u5c0a\u91cd\u5176\u7248\u6743\u548c\u8bb8\u53ef\u8bc1\u3002\u5982\u679c\u60a8\u6253\u7b97\u5728\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\u4f7f\u7528\u6216\u4fee\u6539\u8fd9\u4e9b\u6e90\u7801\uff0c\u8bf7\u9075\u5b88\u76f8\u5e94\u7684\u8bb8\u53ef\u8bc1\u8981\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4e86\u89e3\u7248\u672c\u5dee\u5f02<\/strong>\uff1a\u4e0d\u540c\u7248\u672c\u7684\u5305\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u3002\u5982\u679c\u60a8\u67e5\u770b\u7684\u662f\u67d0\u4e2a\u7279\u5b9a\u7248\u672c\u7684\u6e90\u7801\uff0c\u8bf7\u786e\u4fdd\u60a8\u5b89\u88c5\u7684\u7248\u672c\u4e0e\u67e5\u770b\u7684\u6e90\u7801\u7248\u672c\u4e00\u81f4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9605\u8bfb\u6587\u6863<\/strong>\uff1a\u8bb8\u591a\u7b2c\u4e09\u65b9\u5305\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u89e3\u91ca\u5176\u8bbe\u8ba1\u548c\u5b9e\u73b0\u7ec6\u8282\u3002\u9605\u8bfb\u8fd9\u4e9b\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u6e90\u7801\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u5341\u4e00\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u7b54<\/h3>\n<\/p>\n<p><h4>1. \u5982\u4f55\u67e5\u770b\u5185\u7f6e\u51fd\u6570\u7684\u6e90\u7801\uff1f<\/h4>\n<\/p>\n<p><p>\u5185\u7f6e\u51fd\u6570\uff08\u5982<code>len()<\/code>\u3001<code>print()<\/code>\u7b49\uff09\u7684\u5b9e\u73b0\u901a\u5e38\u5728Python\u7684C\u6e90\u7801\u4e2d\u3002\u60a8\u53ef\u4ee5\u4e0b\u8f7dPython\u89e3\u91ca\u5668\u7684\u6e90\u7801\uff0c\u5e76\u5728<code>Objects<\/code>\u548c<code>Python<\/code>\u76ee\u5f55\u4e2d\u627e\u5230\u76f8\u5e94\u7684\u5b9e\u73b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2. \u5982\u4f55\u67e5\u770b\u52a8\u6001\u751f\u6210\u7684\u51fd\u6570\u7684\u6e90\u7801\uff1f<\/h4>\n<\/p>\n<p><p>\u67d0\u4e9b\u51fd\u6570\u662f\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u7684\uff0c\u4f8b\u5982\u901a\u8fc7<code>exec()<\/code>\u6216<code>eval()<\/code>\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u51fd\u6570\uff0c<code>inspect.getsource()<\/code>\u53ef\u80fd\u65e0\u6cd5\u83b7\u53d6\u5176\u6e90\u7801\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u60a8\u9700\u8981\u627e\u5230\u751f\u6210\u8fd9\u4e9b\u51fd\u6570\u7684\u4ee3\u7801\u6bb5\uff0c\u5e76\u67e5\u770b\u5176\u751f\u6210\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><h4>3. \u5982\u4f55\u67e5\u770b\u5728Jupyter Notebook\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u7684\u6e90\u7801\uff1f<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\uff0c\u5176\u6e90\u7801\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u67e5\u770b\u8fd9\u4e9b\u51fd\u6570\u7684\u6e90\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def some_function():<\/p>\n<p>    pass<\/p>\n<h2><strong>\u83b7\u53d6\u51fd\u6570\u7684\u6e90\u7801<\/strong><\/h2>\n<p>source_code = inspect.getsource(some_function)<\/p>\n<h2><strong>\u6253\u5370\u6e90\u7801<\/strong><\/h2>\n<p>print(source_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528Git\u67e5\u770b\u7248\u672c\u5386\u53f2<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u5728\u67e5\u770b\u67d0\u4e2a\u5305\u7684\u6e90\u7801\u65f6\uff0c\u60f3\u4e86\u89e3\u5176\u4e0d\u540c\u7248\u672c\u4e4b\u95f4\u7684\u53d8\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528Git\u6765\u67e5\u770b\u7248\u672c\u5386\u53f2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684Git\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\"># \u67e5\u770b\u63d0\u4ea4\u5386\u53f2<\/p>\n<p>git log<\/p>\n<h2><strong>\u67e5\u770b\u67d0\u4e2a\u6587\u4ef6\u7684\u5386\u53f2<\/strong><\/h2>\n<p>git log path\/to\/file<\/p>\n<h2><strong>\u67e5\u770b\u67d0\u4e2a\u63d0\u4ea4\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>git show commit_hash<\/p>\n<h2><strong>\u6bd4\u8f83\u4e24\u4e2a\u63d0\u4ea4\u4e4b\u95f4\u7684\u5dee\u5f02<\/strong><\/h2>\n<p>git diff commit_hash1 commit_hash2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u67e5\u770b\u7248\u672c\u5386\u53f2\uff0c\u60a8\u53ef\u4ee5\u4e86\u89e3\u5305\u7684\u6f14\u53d8\u8fc7\u7a0b\uff0c\u53d1\u73b0\u4fee\u590d\u7684bug\u548c\u5f15\u5165\u7684\u65b0\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u67e5\u770bPython\u5305\u7684\u6e90\u7801\u662f\u4e00\u79cd\u975e\u5e38\u6709\u7528\u7684\u6280\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u6df1\u5165\u5730\u7406\u89e3\u5305\u7684\u5b9e\u73b0\u7ec6\u8282\u548c\u5de5\u4f5c\u539f\u7406\u3002<strong>\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u3001\u4f7f\u7528<code>source<\/code>\u547d\u4ee4\u3001\u76f4\u63a5\u67e5\u770b\u5b89\u88c5\u76ee\u5f55\u4e2d\u7684\u6e90\u7801\u6587\u4ef6<\/strong>\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u9664\u4e86\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528IDE\u3001\u5728\u7ebf\u4ee3\u7801\u5e93\u3001\u6587\u6863\u5de5\u5177\u7b49\u6765\u67e5\u770b\u6e90\u7801\u3002\u5bf9\u4e8eC\u6269\u5c55\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u67e5\u770b\u5176C\u6e90\u7801\u6587\u4ef6\u5e76\u9605\u8bfb\u76f8\u5173\u6587\u6863\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u5b66\u4e60\u548c\u4f7f\u7528Python\u5305\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u627e\u5230Python\u5305\u7684\u6e90\u7801\u4f4d\u7f6e\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u5305\u7684\u6e90\u7801\uff0c\u9996\u5148\u9700\u8981\u786e\u5b9a\u8be5\u5305\u7684\u5b89\u88c5\u4f4d\u7f6e\u3002\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c<code>pip show \u5305\u540d<\/code>\uff0c\u5176\u4e2d\u201c\u5305\u540d\u201d\u662f\u4f60\u60f3\u8981\u67e5\u770b\u7684\u5305\u7684\u540d\u79f0\u3002\u8be5\u547d\u4ee4\u4f1a\u663e\u793a\u5305\u7684\u5143\u6570\u636e\uff0c\u5305\u62ec\u5b89\u88c5\u8def\u5f84\u3002\u627e\u5230\u201cLocation\u201d\u5b57\u6bb5\u540e\uff0c\u8fdb\u5165\u8be5\u76ee\u5f55\u5373\u53ef\u627e\u5230\u5305\u7684\u6e90\u7801\u6587\u4ef6\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u76f4\u63a5\u5728Python\u73af\u5883\u4e2d\u67e5\u770b\u5305\u7684\u6e90\u7801\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u73af\u5883\u4e2d\u53ef\u4ee5\u76f4\u63a5\u67e5\u770b\u5305\u7684\u6e90\u7801\u3002\u4f7f\u7528<code>help()<\/code>\u51fd\u6570\u6216<code>pydoc<\/code>\u547d\u4ee4\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u4e86\u89e3\u67d0\u4e2a\u6a21\u5757\u6216\u5305\u7684\u6587\u6863\u548c\u6e90\u7801\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>help(\u4f60\u7684\u5305\u540d)<\/code>\u53ef\u4ee5\u67e5\u770b\u8be5\u5305\u7684\u4f7f\u7528\u8bf4\u660e\u548c\u90e8\u5206\u6e90\u7801\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5176\u529f\u80fd\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770bPython\u5305\u7684\u6e90\u7801\uff1f<\/strong><br \/>\u591a\u79cd\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u67e5\u770bPython\u5305\u7684\u6e90\u7801\u3002\u5176\u4e2d\uff0cIPython\u548cJupyter Notebook\u90fd\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>??<\/code>\u64cd\u4f5c\u7b26\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u6e90\u4ee3\u7801\u3002\u53e6\u5916\uff0c\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u6216Visual Studio Code\uff0c\u4e5f\u80fd\u8f7b\u677e\u5730\u6d4f\u89c8\u548c\u8c03\u8bd5Python\u5305\u7684\u6e90\u7801\uff0c\u751a\u81f3\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u76f8\u5173\u7684\u51fd\u6570\u6216\u7c7b\u5b9a\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u67e5\u770bPython\u5305\u7684\u6e90\u7801\uff1a\u4f7f\u7528inspect\u6a21\u5757\u3001\u4f7f\u7528source\u547d\u4ee4\u3001\u76f4\u63a5\u67e5\u770b\u5b89\u88c5\u76ee\u5f55\u4e2d\u7684\u6e90\u7801\u6587\u4ef6\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1168273,"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\/1168269"}],"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=1168269"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168269\/revisions"}],"predecessor-version":[{"id":1168274,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1168269\/revisions\/1168274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1168273"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1168269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1168269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1168269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}