{"id":1109090,"date":"2025-01-08T17:06:43","date_gmt":"2025-01-08T09:06:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1109090.html"},"modified":"2025-01-08T17:06:46","modified_gmt":"2025-01-08T09:06:46","slug":"%e5%a6%82%e4%bd%95%e7%9c%8bpython%e4%b8%ad%e6%9f%90%e4%b8%aa%e5%ba%93%e5%87%bd%e6%95%b0%e7%9a%84%e5%ae%9a%e4%b9%89-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1109090.html","title":{"rendered":"\u5982\u4f55\u770bPython\u4e2d\u67d0\u4e2a\u5e93\u51fd\u6570\u7684\u5b9a\u4e49"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072511\/fcf05387-9e6b-4c03-87d2-1663cf0e9179.webp\" alt=\"\u5982\u4f55\u770bPython\u4e2d\u67d0\u4e2a\u5e93\u51fd\u6570\u7684\u5b9a\u4e49\" \/><\/p>\n<p><p> <strong>\u8981\u67e5\u770bPython\u4e2d\u67d0\u4e2a\u5e93\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>help()<\/code>\u3001\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570\u3001\u67e5\u770b\u5e93\u7684\u5b98\u65b9\u6587\u6863\u3001\u67e5\u770b\u6e90\u4ee3\u7801\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u8be6\u7ec6\u7684\u65b9\u6cd5\u662f\u67e5\u770b\u5e93\u7684\u5b98\u65b9\u6587\u6863\u548c\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>help()<\/code><\/h3>\n<\/p>\n<p><p>\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\u662f\u67e5\u770bPython\u5bf9\u8c61\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u89e3\u91ca\u7684\u4e00\u4e2a\u7b80\u5355\u65b9\u5f0f\u3002\u5b83\u63d0\u4f9b\u4e86\u51fd\u6570\u3001\u7c7b\u548c\u6a21\u5757\u7684\u8be6\u7ec6\u8bf4\u660e\uff0c\u5305\u62ec\u53c2\u6570\u3001\u8fd4\u56de\u503c\u548c\u7528\u6cd5\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>help(math.sqrt)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>math.sqrt<\/code>\u51fd\u6570\u7684\u6587\u6863\uff0c\u5305\u62ec\u5b83\u7684\u7528\u9014\u548c\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>dir()<\/code>\u51fd\u6570\u5217\u51fa\u4e86\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5bf9\u4e8e\u6a21\u5757\uff0c\u5b83\u5217\u51fa\u4e86\u6240\u6709\u5b9a\u4e49\u7684\u51fd\u6570\u548c\u7c7b\u3002\u5c3d\u7ba1<code>dir()<\/code>\u4e0d\u4f1a\u63d0\u4f9b\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u4f46\u5b83\u662f\u4e00\u4e2a\u5feb\u901f\u4e86\u89e3\u6a21\u5757\u5185\u5bb9\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(dir(math))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>math<\/code>\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u67e5\u770b\u5e93\u7684\u5b98\u65b9\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u5b98\u65b9\u6587\u6863\u901a\u5e38\u662f\u6700\u5168\u9762\u548c\u51c6\u786e\u7684\u8d44\u6e90\u3002\u5927\u591a\u6570\u6d41\u884c\u7684Python\u5e93\u90fd\u6709\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u89e3\u91ca\u6bcf\u4e2a\u51fd\u6570\u7684\u7528\u9014\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><p>\u8bbf\u95eePython\u6807\u51c6\u5e93\u7684\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/docs.python.org\/3\/library\/\">Python Standard Library Documentation<\/a><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u67e5\u770b\u6e90\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u67e5\u770b\u6e90\u4ee3\u7801\u662f\u6700\u5f7b\u5e95\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5bf9\u4e8e\u5f00\u6e90\u5e93\u3002\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u67e5\u770b\u5e93\u7684\u6e90\u4ee3\u7801\uff0c\u6216\u8005\u5728GitHub\u7b49\u4ee3\u7801\u6258\u7ba1\u5e73\u53f0\u4e0a\u67e5\u627e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import math<\/p>\n<p>print(inspect.getsource(math.sqrt))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u5c1d\u8bd5\u8f93\u51fa<code>math.sqrt<\/code>\u7684\u6e90\u4ee3\u7801\uff0c\u4f46\u8bf7\u6ce8\u610f\uff0c\u6709\u4e9b\u5185\u7f6e\u6a21\u5757\u662f\u7528C\u7f16\u5199\u7684\uff0c\u65e0\u6cd5\u7528\u8fd9\u79cd\u65b9\u6cd5\u67e5\u770b\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\u6765\u83b7\u53d6\u6d3b\u8dc3\u5bf9\u8c61\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u6a21\u5757\u3001\u7c7b\u3001\u65b9\u6cd5\u3001\u51fd\u6570\u3001\u4ee3\u7801\u5bf9\u8c61\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import math<\/p>\n<p>print(inspect.getdoc(math.sqrt))<\/p>\n<p>print(inspect.signature(math.sqrt))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>math.sqrt<\/code>\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u51fd\u6570\u7b7e\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528IDE\u6216\u4ee3\u7801\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><p>\u73b0\u4ee3\u7684IDE\uff08\u5982PyCharm\u3001VSCode\uff09\u901a\u5e38\u90fd\u6709\u5f3a\u5927\u7684\u4ee3\u7801\u5bfc\u822a\u548c\u6587\u6863\u67e5\u770b\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u53f3\u952e\u70b9\u51fb\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u7136\u540e\u9009\u62e9\u201c\u67e5\u770b\u5b9a\u4e49\u201d\u6216\u7c7b\u4f3c\u9009\u9879\uff0c\u76f4\u63a5\u8df3\u8f6c\u5230\u51fd\u6570\u7684\u5b9a\u4e49\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5177\u4f53\u793a\u4f8b\uff1a\u67e5\u770bNumPy\u5e93\u4e2d\u67d0\u4e2a\u51fd\u6570\u7684\u5b9a\u4e49<\/h3>\n<\/p>\n<p><h4>\u793a\u4f8b\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>help(np.array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>np.array<\/code>\u51fd\u6570\u7684\u8be6\u7ec6\u6587\u6863\uff0c\u5305\u62ec\u53c2\u6570\u3001\u8fd4\u56de\u503c\u548c\u7528\u6cd5\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u67e5\u770bNumPy\u7684\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/numpy.org\/doc\/stable\/\">NumPy Documentation<\/a><\/p>\n<\/p>\n<p><p>\u6216\u8005\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>print(inspect.getdoc(np.array))<\/p>\n<p>print(inspect.signature(np.array))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa<code>np.array<\/code>\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u51fd\u6570\u7b7e\u540d\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0c\u67e5\u770bPython\u4e2d\u67d0\u4e2a\u5e93\u51fd\u6570\u7684\u5b9a\u4e49\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<code>help()<\/code>\u548c<code>dir()<\/code>\u3001\u67e5\u770b\u5b98\u65b9\u6587\u6863\u3001\u67e5\u770b\u6e90\u4ee3\u7801\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u4ee5\u53ca\u4f7f\u7528\u73b0\u4ee3IDE\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5bf9\u4e8e\u8be6\u7ec6\u548c\u6743\u5a01\u7684\u4fe1\u606f\uff0c\u67e5\u770b\u5b98\u65b9\u6587\u6863\u548c\u6e90\u4ee3\u7801\u662f\u6700\u53ef\u9760\u7684\u9014\u5f84\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u627ePython\u5e93\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u5e93\u4e2d\u67d0\u4e2a\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u8fdb\u884c\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u63d0\u4f9b\u5173\u4e8e\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u4fe1\u606f\u3002\u53ef\u4ee5\u5728Python\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u8f93\u5165<code>help(\u5e93\u540d.\u51fd\u6570\u540d)<\/code>\uff0c\u8fd9\u5c06\u663e\u793a\u8be5\u51fd\u6570\u7684\u8be6\u7ec6\u63cf\u8ff0\u548c\u4f7f\u7528\u793a\u4f8b\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570\u53ef\u4ee5\u5217\u51fa\u5e93\u4e2d\u7684\u6240\u6709\u53ef\u7528\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e2e\u52a9\u60a8\u627e\u5230\u6240\u9700\u7684\u51fd\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528IDE\u67e5\u770b\u51fd\u6570\u5b9a\u4e49\uff1f<\/strong><br \/>\u73b0\u4ee3\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u3001VS Code\u7b49\uff0c\u63d0\u4f9b\u4e86\u4fbf\u6377\u7684\u529f\u80fd\u6765\u67e5\u770b\u51fd\u6570\u7684\u5b9a\u4e49\u3002\u53ea\u9700\u5c06\u5149\u6807\u60ac\u505c\u5728\u51fd\u6570\u540d\u4e0a\uff0cIDE\u901a\u5e38\u4f1a\u81ea\u52a8\u663e\u793a\u51fd\u6570\u7684\u6587\u6863\u4fe1\u606f\u548c\u5b9a\u4e49\u4f4d\u7f6e\u3002\u53f3\u952e\u70b9\u51fb\u51fd\u6570\u540d\uff0c\u9009\u62e9\u201c\u8df3\u8f6c\u5230\u5b9a\u4e49\u201d\u6216\u7c7b\u4f3c\u9009\u9879\uff0c\u53ef\u4ee5\u76f4\u63a5\u8df3\u8f6c\u5230\u8be5\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u6587\u6863\u627e\u5230Python\u5e93\u51fd\u6570\u7684\u5b9a\u4e49\uff1f<\/strong><br \/>\u5927\u591a\u6570Python\u5e93\u90fd\u6709\u5b98\u65b9\u6587\u6863\uff0c\u5305\u542b\u51fd\u6570\u7684\u8be6\u7ec6\u5b9a\u4e49\u548c\u4f7f\u7528\u793a\u4f8b\u3002\u60a8\u53ef\u4ee5\u8bbf\u95ee\u5e93\u7684\u5b98\u65b9\u7f51\u7ad9\u6216GitHub\u9875\u9762\uff0c\u901a\u5e38\u5728\u201c\u6587\u6863\u201d\u90e8\u5206\u53ef\u4ee5\u627e\u5230\u51fd\u6570\u7684\u8be6\u7ec6\u8bf4\u660e\u3002\u4f7f\u7528\u641c\u7d22\u529f\u80fd\uff0c\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u7279\u5b9a\u51fd\u6570\u7684\u5b9a\u4e49\u53ca\u5176\u53c2\u6570\u3001\u8fd4\u56de\u503c\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u67e5\u770bPython\u4e2d\u67d0\u4e2a\u5e93\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u51fd\u6570help()\u3001\u4f7f\u7528dir()\u51fd\u6570\u3001\u67e5\u770b [&hellip;]","protected":false},"author":3,"featured_media":1109095,"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\/1109090"}],"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=1109090"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109090\/revisions"}],"predecessor-version":[{"id":1109098,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1109090\/revisions\/1109098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1109095"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1109090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1109090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1109090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}