{"id":999725,"date":"2024-12-27T09:43:52","date_gmt":"2024-12-27T01:43:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999725.html"},"modified":"2024-12-27T09:43:54","modified_gmt":"2024-12-27T01:43:54","slug":"%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8bpython%e5%87%bd%e6%95%b0%e6%ba%90%e7%a0%81","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999725.html","title":{"rendered":"\u5982\u4f55\u67e5\u770bpython\u51fd\u6570\u6e90\u7801"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074625\/6781a088-0b32-4752-86dd-763c6eec2315.webp\" alt=\"\u5982\u4f55\u67e5\u770bpython\u51fd\u6570\u6e90\u7801\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u770b\u51fd\u6570\u6e90\u7801\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528inspect\u6a21\u5757\u3001\u4f7f\u7528source\u6a21\u5757\u3001\u67e5\u770b\u5e93\u7684\u6587\u6863\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528inspect\u6a21\u5757<\/strong>\u662f\u6700\u4e3a\u5e38\u7528\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002inspect\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u83b7\u53d6\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u51fd\u6570\u7684\u6e90\u7801\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528INSPECT\u6a21\u5757\u67e5\u770b\u6e90\u7801<\/h3>\n<\/p>\n<p><h4>1. Inspect\u6a21\u5757\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684inspect\u6a21\u5757\u662f\u4e00\u4e2a\u5185\u7f6e\u6a21\u5757\uff0c\u4e13\u95e8\u7528\u4e8e\u83b7\u53d6\u6d3b\u8dc3\u5bf9\u8c61\u7684\u4fe1\u606f\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u7c7b\u3001\u51fd\u6570\u3001\u6a21\u5757\u548c\u65b9\u6cd5\u3002\u4f7f\u7528inspect\u6a21\u5757\uff0c\u6211\u4eec\u4e0d\u4ec5\u53ef\u4ee5\u67e5\u770b\u51fd\u6570\u7684\u6e90\u7801\uff0c\u8fd8\u53ef\u4ee5\u83b7\u53d6\u51fd\u6570\u7684\u53c2\u6570\u3001\u8fd4\u56de\u503c\u6ce8\u91ca\u7b49\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>2. \u83b7\u53d6\u51fd\u6570\u6e90\u7801<\/h4>\n<\/p>\n<p><p>\u8981\u67e5\u770b\u4e00\u4e2aPython\u51fd\u6570\u7684\u6e90\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528inspect\u6a21\u5757\u7684<code>getsource()<\/code>\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u8be5\u5bf9\u8c61\u7684\u6e90\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def sample_function(x, y):<\/p>\n<p>    &quot;&quot;&quot;This is a sample function.&quot;&quot;&quot;<\/p>\n<p>    return x + y<\/p>\n<h2><strong>\u83b7\u53d6\u51fd\u6570\u6e90\u7801<\/strong><\/h2>\n<p>source_code = inspect.getsource(sample_function)<\/p>\n<p>print(source_code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>getsource()<\/code>\u51fd\u6570\u8fd4\u56de\u4e86<code>sample_function<\/code>\u51fd\u6570\u7684\u6e90\u7801\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>3. \u5904\u7406\u5185\u7f6e\u51fd\u6570\u548cC\u8bed\u8a00\u5b9e\u73b0\u7684\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cinspect\u6a21\u5757\u53ea\u80fd\u7528\u4e8e\u67e5\u770bPython\u8bed\u8a00\u7f16\u5199\u7684\u51fd\u6570\u7684\u6e90\u7801\u3002\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u662f\u7528C\u8bed\u8a00\u5b9e\u73b0\u7684\uff08\u4f8b\u5982\u5927\u591a\u6570\u5185\u7f6e\u51fd\u6570\uff09\uff0c<code>getsource()<\/code>\u51fd\u6570\u5c06\u4f1a\u629b\u51fa<code>OSError<\/code>\u5f02\u5e38\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u67e5\u770b\u51fd\u6570\u7684\u5b98\u65b9\u6587\u6863\u6216\u8005\u6e90\u7801\u4ed3\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528SOURCE\u6a21\u5757\uff08IPython\uff09<\/h3>\n<\/p>\n<p><h4>1. IPython\u4e2d\u7684\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528IPython\uff08\u5982Jupyter Notebook\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>??<\/code>\u547d\u4ee4\u67e5\u770b\u51fd\u6570\u7684\u6e90\u7801\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u529f\u80fd\uff0c\u7279\u522b\u662f\u5f53\u4f60\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u5de5\u4f5c\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sample_function(x, y):<\/p>\n<p>    &quot;&quot;&quot;This is a sample function.&quot;&quot;&quot;<\/p>\n<p>    return x + y<\/p>\n<h2><strong>\u5728IPython\u4e2d\u4f7f\u7528<\/strong><\/h2>\n<p>sample_function??<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728IPython\u4e2d\u8f93\u5165\u4e0a\u8ff0\u4ee3\u7801\uff0c\u5c06\u4f1a\u663e\u793a<code>sample_function<\/code>\u7684\u6e90\u7801\u548c\u6587\u6863\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u67e5\u770b\u5e93\u7684\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\uff0c\u7279\u522b\u662f\u90a3\u4e9b\u7528C\u8bed\u8a00\u5b9e\u73b0\u7684\u5e93\uff0c\u6211\u4eec\u53ef\u80fd\u65e0\u6cd5\u76f4\u63a5\u67e5\u770b\u6e90\u7801\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u67e5\u770b\u5e93\u7684\u5b98\u65b9\u6587\u6863\u6216\u8005\u5176\u6e90\u7801\u4ed3\u5e93\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b98\u65b9\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5927\u591a\u6570Python\u5e93\u90fd\u6709\u8be6\u7ec6\u7684\u5b98\u65b9\u6587\u6863\u3002\u5728\u6587\u6863\u4e2d\uff0c\u901a\u5e38\u4f1a\u6709\u51fd\u6570\u7684\u4f7f\u7528\u793a\u4f8b\u3001\u53c2\u6570\u8bf4\u660e\u548c\u8fd4\u56de\u503c\u89e3\u91ca\u3002\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<\/p>\n<p><h4>2. \u6e90\u7801\u4ed3\u5e93<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4e00\u4e2a\u5e93\u662f\u5f00\u6e90\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5728GitHub\u7b49\u5e73\u53f0\u4e0a\u627e\u5230\u5b83\u7684\u6e90\u7801\u4ed3\u5e93\u3002\u5728\u6e90\u7801\u4ed3\u5e93\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u5e93\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u751a\u81f3\u53ef\u4ee5\u4e0b\u8f7d\u6e90\u7801\u8fdb\u884c\u672c\u5730\u9605\u8bfb\u548c\u4fee\u6539\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528PYTHON\u5185\u7f6e\u5e2e\u52a9\u7cfb\u7edf<\/h3>\n<\/p>\n<p><h4>1. help()\u51fd\u6570<\/h4>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u5e2e\u52a9\u4fe1\u606f\uff0c\u5305\u62ec\u6587\u6863\u5b57\u7b26\u4e32\u548c\u51fd\u6570\u7b7e\u540d\u3002\u5c3d\u7ba1<code>help()<\/code>\u51fd\u6570\u4e0d\u4f1a\u76f4\u63a5\u663e\u793a\u6e90\u7801\uff0c\u4f46\u5b83\u53ef\u4ee5\u63d0\u4f9b\u6709\u5173\u51fd\u6570\u7684\u6709\u7528\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def sample_function(x, y):<\/p>\n<p>    &quot;&quot;&quot;This is a sample function.&quot;&quot;&quot;<\/p>\n<p>    return x + y<\/p>\n<h2><strong>\u4f7f\u7528help()\u51fd\u6570<\/strong><\/h2>\n<p>help(sample_function)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>help()<\/code>\u51fd\u6570\u5c06\u663e\u793a<code>sample_function<\/code>\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u53c2\u6570\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528THIRD-PARTY\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528Pycharm\u7b49IDE<\/h4>\n<\/p>\n<p><p>\u4e00\u4e9b\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VSCode\u7b49\uff0c\u63d0\u4f9b\u4e86\u67e5\u770b\u51fd\u6570\u6e90\u7801\u7684\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5feb\u6377\u952e\u6216\u83dc\u5355\u9009\u9879\u76f4\u63a5\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>2. \u67e5\u770b\u4ee3\u7801\u9759\u6001\u5206\u6790\u5de5\u5177<\/h4>\n<\/p>\n<p><p>\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u4ee3\u7801\u9759\u6001\u5206\u6790\uff0c\u4ece\u800c\u67e5\u770b\u51fd\u6570\u7684\u8c03\u7528\u5173\u7cfb\u548c\u5b9e\u73b0\u7ec6\u8282\u3002\u4f8b\u5982\uff0cPyLint\u3001Flake8\u7b49\u5de5\u5177\u53ef\u4ee5\u5206\u6790\u4ee3\u7801\u7ed3\u6784\uff0c\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u51fd\u6570\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u67e5\u770b\u51fd\u6570\u7684\u6e90\u7801\u3002<strong>\u4f7f\u7528inspect\u6a21\u5757\u3001\u4f7f\u7528source\u6a21\u5757\u3001\u67e5\u770b\u5e93\u7684\u6587\u6863<\/strong>\u90fd\u662f\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u8c03\u8bd5\u4ee3\u7801\uff0c\u63d0\u9ad8\u7f16\u7a0b\u6548\u7387\u3002\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u6df1\u5165\u5730\u4e86\u89e3Python\u4ee3\u7801\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u4ece\u800c\u63d0\u9ad8\u6211\u4eec\u7684\u7f16\u7a0b\u6280\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u627e\u5230\u7279\u5b9aPython\u5e93\u4e2d\u51fd\u6570\u7684\u6e90\u7801\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u7279\u5b9a\u5e93\u4e2d\u7684\u51fd\u6570\u6e90\u7801\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>help()<\/code>\uff0c\u7ed3\u5408<code>inspect<\/code>\u6a21\u5757\u4e2d\u7684<code>getsource()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u9996\u5148\u5bfc\u5165\u5e93\uff0c\u7136\u540e\u8c03\u7528<code>help(library.function)<\/code>\u4ee5\u83b7\u53d6\u6587\u6863\uff0c\u63a5\u7740\u4f7f\u7528<code>inspect.getsource(library.function)<\/code>\u6765\u67e5\u770b\u6e90\u7801\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5927\u591a\u6570\u5f00\u6e90\u5e93\uff0c\u4f46\u67d0\u4e9b\u5e93\u53ef\u80fd\u4f1a\u4ee5C\u8bed\u8a00\u7f16\u5199\u6269\u5c55\uff0c\u65e0\u6cd5\u76f4\u63a5\u67e5\u770b\u6e90\u7801\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u7ebf\u67e5\u770bPython\u51fd\u6570\u7684\u6e90\u7801\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u8bb8\u591a\u6d41\u884c\u7684Python\u5e93\u90fd\u6258\u7ba1\u5728GitHub\u7b49\u5e73\u53f0\u4e0a\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u5728\u8fd9\u4e9b\u7f51\u7ad9\u4e0a\u641c\u7d22\u5e93\u540d\u548c\u51fd\u6570\u540d\uff0c\u4ee5\u627e\u5230\u76f8\u5e94\u7684\u6e90\u7801\u3002\u6b64\u5916\uff0c\u50cfRead the Docs\u8fd9\u6837\u7684\u5e73\u53f0\u4e5f\u63d0\u4f9b\u4e86\u8bb8\u591a\u5e93\u7684\u6587\u6863\uff0c\u5176\u4e2d\u901a\u5e38\u5305\u542b\u6e90\u7801\u94fe\u63a5\uff0c\u4fbf\u4e8e\u7528\u6237\u67e5\u770b\u548c\u5b66\u4e60\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u770b\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u6e90\u7801\uff1f<\/strong><br \/>\u5bf9\u4e8e\u60a8\u81ea\u5df1\u7f16\u5199\u7684\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u76f4\u63a5\u5728\u4ee3\u7801\u7f16\u8f91\u5668\u4e2d\u627e\u5230\u5b83\u7684\u5b9a\u4e49\u3002\u5982\u679c\u60a8\u5e0c\u671b\u5728\u8fd0\u884c\u65f6\u67e5\u770b\u81ea\u5b9a\u4e49\u51fd\u6570\u7684\u6e90\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528<code>inspect.getsource()<\/code>\u51fd\u6570\u3002\u786e\u4fdd\u5728\u51fd\u6570\u5b9a\u4e49\u7684\u540c\u4e00\u4f5c\u7528\u57df\u4e0b\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u4fbf\u53ef\u8f7b\u677e\u83b7\u53d6\u5176\u6e90\u7801\u3002\u8fd9\u5bf9\u8c03\u8bd5\u6216\u91cd\u6784\u4ee3\u7801\u65f6\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u770b\u51fd\u6570\u6e90\u7801\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528inspect\u6a21\u5757\u3001\u4f7f\u7528source\u6a21\u5757\u3001\u67e5\u770b\u5e93\u7684\u6587\u6863\u3002 [&hellip;]","protected":false},"author":3,"featured_media":999730,"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\/999725"}],"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=999725"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999725\/revisions"}],"predecessor-version":[{"id":999733,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999725\/revisions\/999733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999730"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}