{"id":943535,"date":"2024-12-26T22:42:29","date_gmt":"2024-12-26T14:42:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/943535.html"},"modified":"2024-12-26T22:42:31","modified_gmt":"2024-12-26T14:42:31","slug":"python%e5%a6%82%e4%bd%95%e7%9c%8b%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/943535.html","title":{"rendered":"python\u5982\u4f55\u770b\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080915\/196407f7-547b-44fc-a267-04ad8a80f181.webp\" alt=\"python\u5982\u4f55\u770b\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u67e5\u770b\u51fd\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\u3001\u67e5\u770b\u51fd\u6570\u7684<code>__doc__<\/code>\u5c5e\u6027\u3001\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570\u3001\u5229\u7528<code>inspect<\/code>\u6a21\u5757\u3002<\/strong>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u67e5\u770b\u51fd\u6570\u7684\u5b9a\u4e49\u3001\u6587\u6863\u5b57\u7b26\u4e32\u548c\u53c2\u6570\u5217\u8868\u7b49\u4fe1\u606f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u67e5\u770bPython\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>help()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>help()<\/code>\u51fd\u6570\u662fPython\u4e2d\u7528\u4e8e\u67e5\u770b\u5bf9\u8c61\u3001\u6a21\u5757\u6216\u51fd\u6570\u5e2e\u52a9\u4fe1\u606f\u7684\u5185\u7f6e\u51fd\u6570\u3002\u901a\u8fc7\u8c03\u7528<code>help()<\/code>\u5e76\u4f20\u5165\u51fd\u6570\u540d\uff0c\u60a8\u53ef\u4ee5\u67e5\u770b\u8be5\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u53c2\u6570\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function(param1, param2):<\/p>\n<p>    &quot;&quot;&quot;This is an example function that adds two numbers.&quot;&quot;&quot;<\/p>\n<p>    return param1 + param2<\/p>\n<p>help(example_function)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>help(example_function)<\/code>\u4f1a\u8f93\u51fa\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff1a\u201cThis is an example function that adds two numbers.\u201d\uff0c\u5e76\u5c55\u793a\u51fd\u6570\u7684\u53c2\u6570\u4fe1\u606f\u3002<code>help()<\/code>\u51fd\u6570\u662f\u4e86\u89e3\u51fd\u6570\u7528\u9014\u548c\u4f7f\u7528\u65b9\u6cd5\u7684\u5feb\u901f\u9014\u5f84\uff0c\u5c24\u5176\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u5e2e\u52a9\u6587\u6863\u80fd\u591f\u5feb\u901f\u6307\u5bfc\u6211\u4eec\u4f7f\u7528\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u67e5\u770b<code>__doc__<\/code>\u5c5e\u6027<\/p>\n<\/p>\n<p><p>\u6bcf\u4e2aPython\u51fd\u6570\u90fd\u6709\u4e00\u4e2a<code>__doc__<\/code>\u5c5e\u6027\uff0c\u5b83\u7528\u4e8e\u5b58\u50a8\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002\u901a\u8fc7\u8bbf\u95ee\u8fd9\u4e2a\u5c5e\u6027\uff0c\u53ef\u4ee5\u76f4\u63a5\u67e5\u770b\u51fd\u6570\u7684\u63cf\u8ff0\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(example_function.__doc__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u5feb\u901f\u67e5\u770b\u5355\u4e2a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u4f46\u4e0d\u63d0\u4f9b\u53c2\u6570\u548c\u7c7b\u578b\u4fe1\u606f\u3002\u7ed3\u5408<code>help()<\/code>\u51fd\u6570\u4f7f\u7528\uff0c\u53ef\u4ee5\u66f4\u5168\u9762\u5730\u4e86\u89e3\u51fd\u6570\u7684\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>dir()<\/code>\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5217\u8868\u3002\u5bf9\u4e8e\u51fd\u6570\u5bf9\u8c61\uff0c<code>dir()<\/code>\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u67e5\u770b\u5b83\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(dir(example_function))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>dir()<\/code>\u4e0d\u80fd\u76f4\u63a5\u63d0\u4f9b\u51fd\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f46\u5b83\u80fd\u5e2e\u52a9\u6211\u4eec\u627e\u5230\u76f8\u5173\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u8f85\u52a9\u8c03\u8bd5\u548c\u5f00\u53d1\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5229\u7528<code>inspect<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p><code>inspect<\/code>\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u4e13\u95e8\u7528\u4e8e\u83b7\u53d6\u6d3b\u8dc3\u5bf9\u8c61\u7684\u4fe1\u606f\u3002\u901a\u8fc7<code>inspect<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u51fd\u6570\u7684\u53c2\u6570\u3001\u6e90\u7801\u3001\u7c7b\u548c\u6a21\u5757\u7b49\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>print(inspect.getsource(example_function))<\/p>\n<p>print(inspect.signature(example_function))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>inspect.getsource()<\/code>\u4f1a\u8f93\u51fa\u51fd\u6570\u7684\u6e90\u4ee3\u7801\uff0c\u800c<code>inspect.signature()<\/code>\u5219\u8fd4\u56de\u51fd\u6570\u7684\u53c2\u6570\u7b7e\u540d\u3002\u5bf9\u4e8e\u9700\u8981\u6df1\u5165\u4e86\u89e3\u51fd\u6570\u5b9e\u73b0\u548c\u53c2\u6570\u7ec6\u8282\u7684\u5f00\u53d1\u8005\uff0c<code>inspect<\/code>\u6a21\u5757\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528IDE\u6216\u7f16\u8f91\u5668\u7684\u529f\u80fd<\/p>\n<\/p>\n<p><p>\u73b0\u4ee3\u7684IDE\uff08\u5982PyCharm\u3001Visual Studio Code\uff09\u901a\u5e38\u63d0\u4f9b\u5f3a\u5927\u7684\u4ee3\u7801\u5206\u6790\u548c\u81ea\u52a8\u63d0\u793a\u529f\u80fd\u3002\u901a\u8fc7IDE\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u51fd\u6570\u5b9a\u4e49\u3001\u6587\u6863\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u4ee3\u7801\u8df3\u8f6c<\/strong>\uff1a\u6309\u4e0b\u7279\u5b9a\u5feb\u6377\u952e\u53ef\u4ee5\u5feb\u901f\u8df3\u8f6c\u5230\u51fd\u6570\u5b9a\u4e49\u4f4d\u7f6e\u3002<\/li>\n<li><strong>\u81ea\u52a8\u63d0\u793a<\/strong>\uff1a\u5728\u8f93\u5165\u51fd\u6570\u540d\u65f6\uff0cIDE\u4f1a\u81ea\u52a8\u663e\u793a\u53c2\u6570\u548c\u6587\u6863\u4fe1\u606f\u3002<\/li>\n<li><strong>\u5185\u7f6e\u6587\u6863\u67e5\u770b<\/strong>\uff1a\u4e00\u4e9bIDE\u63d0\u4f9b\u5185\u7f6e\u7684\u6587\u6863\u67e5\u770b\u7a97\u53e3\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u67e5\u9605\u3002<\/li>\n<\/ul>\n<p><p>\u516d\u3001\u67e5\u770b\u51fd\u6570\u7684\u6d4b\u8bd5\u7528\u4f8b<\/p>\n<\/p>\n<p><p>\u6d4b\u8bd5\u7528\u4f8b\u901a\u5e38\u5305\u542b\u51fd\u6570\u7684\u4f7f\u7528\u793a\u4f8b\uff0c\u901a\u8fc7\u67e5\u770b\u548c\u8fd0\u884c\u6d4b\u8bd5\u7528\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u51fd\u6570\u7684\u9884\u671f\u8f93\u5165\u548c\u8f93\u51fa\u3002\u8fd9\u4e5f\u662f\u7406\u89e3\u51fd\u6570\u884c\u4e3a\u7684\u91cd\u8981\u9014\u5f84\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u9605\u8bfb\u6e90\u7801\u548c\u6ce8\u91ca<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u6e90\u9879\u76ee\u4e2d\uff0c\u67e5\u770b\u51fd\u6570\u7684\u6e90\u7801\u548c\u6ce8\u91ca\u662f\u4e86\u89e3\u5176\u5de5\u4f5c\u539f\u7406\u7684\u91cd\u8981\u65b9\u5f0f\u3002\u826f\u597d\u7684\u6ce8\u91ca\u53ef\u4ee5\u89e3\u91ca\u51fd\u6570\u8bbe\u8ba1\u601d\u8def\u3001\u5173\u952e\u7b97\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5168\u9762\u4e86\u89e3Python\u51fd\u6570\u7684\u5b9a\u4e49\u3001\u6587\u6863\u3001\u53c2\u6570\u548c\u5b9e\u73b0\u7ec6\u8282\u3002\u65e0\u8bba\u662f\u5728\u5b66\u4e60\u65b0\u5e93\u8fd8\u662f\u8c03\u8bd5\u5df2\u6709\u4ee3\u7801\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u90fd\u80fd\u63d0\u5347\u6211\u4eec\u7684\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770bPython\u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\u548c\u6587\u6863\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\uff0c\u4ece\u800c\u4e86\u89e3\u5176\u7528\u9014\u548c\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>help(\u51fd\u6570\u540d)<\/code>\u5373\u53ef\u67e5\u770b\u8be5\u51fd\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>__doc__<\/code>\u5c5e\u6027\u4e5f\u80fd\u83b7\u53d6\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982<code>\u51fd\u6570\u540d.__doc__<\/code>\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u51fd\u6570\u7684\u53c2\u6570\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86<code>inspect<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u51fd\u6570\u7684\u53c2\u6570\u4fe1\u606f\u3002\u4f7f\u7528<code>inspect.signature(\u51fd\u6570\u540d)<\/code>\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2aSignature\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u3001\u9ed8\u8ba4\u503c\u7b49\u4fe1\u606f\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u4e86\u89e3\u51fd\u6570\u7684\u8c03\u7528\u65b9\u5f0f\u548c\u6240\u9700\u53c2\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528IDE\u6216\u7f16\u8f91\u5668\u67e5\u770bPython\u51fd\u6570\u7684\u5b9a\u4e49\uff1f<\/strong><br \/>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u548c\u4ee3\u7801\u7f16\u8f91\u5668\uff08\u5982PyCharm\u3001VSCode\uff09\u90fd\u652f\u6301\u8df3\u8f6c\u5230\u51fd\u6570\u5b9a\u4e49\u7684\u529f\u80fd\u3002\u901a\u8fc7\u5c06\u5149\u6807\u60ac\u505c\u5728\u51fd\u6570\u540d\u4e0a\uff0c\u901a\u5e38\u53ef\u4ee5\u770b\u5230\u51fd\u6570\u7684\u6587\u6863\u6216\u53c2\u6570\u63d0\u793a\u3002\u5982\u679c\u4f7f\u7528\u7684\u662fVSCode\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7Ctrl\u952e+\u70b9\u51fb\u51fd\u6570\u540d\u6765\u76f4\u63a5\u8df3\u8f6c\u5230\u8be5\u51fd\u6570\u7684\u5b9a\u4e49\u4f4d\u7f6e\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u67e5\u770b\u51fd\u6570\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6e\u7684help()\u51fd\u6570\u3001\u67e5\u770b\u51fd\u6570\u7684__doc__\u5c5e\u6027\u3001\u4f7f\u7528dir()\u51fd [&hellip;]","protected":false},"author":3,"featured_media":943537,"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\/943535"}],"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=943535"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943535\/revisions"}],"predecessor-version":[{"id":943541,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/943535\/revisions\/943541"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/943537"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=943535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=943535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=943535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}