{"id":1053862,"date":"2024-12-31T14:37:32","date_gmt":"2024-12-31T06:37:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053862.html"},"modified":"2024-12-31T14:37:33","modified_gmt":"2024-12-31T06:37:33","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%96%87%e6%a1%a3%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053862.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/767f1eda-205c-449b-974e-dcbcb476159e.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u4e2d\u5982\u4f55\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u6587\u6863\u5b57\u7b26\u4e32\u3001\u8bbf\u95ee\u6587\u6863\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u8fdb\u884c\u6ce8\u91ca\u3002<\/strong> \u6587\u6863\u5b57\u7b26\u4e32\uff08Docstrings\uff09\u662fPython\u4e2d\u7528\u4e8e\u4e3a\u6a21\u5757\u3001\u51fd\u6570\u3001\u7c7b\u548c\u65b9\u6cd5\u7f16\u5199\u8bf4\u660e\u6587\u6863\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u4eec\u901a\u5e38\u5199\u5728\u8fd9\u4e9b\u5143\u7d20\u7684\u5f00\u5934\uff0c\u5e2e\u52a9\u7a0b\u5e8f\u5458\u7406\u89e3\u4ee3\u7801\u7684\u7528\u9014\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b9a\u4e49\u6587\u6863\u5b57\u7b26\u4e32<\/strong>\uff1a\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u3001\u7c7b\u6216\u6a21\u5757\u7684\u5f00\u5934\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&#39;&#39;&#39;\u6216&quot;&quot;&quot;\uff09\u6765\u5b9a\u4e49\u6587\u6863\u5b57\u7b26\u4e32\u3002\u8fd9\u4e9b\u5b57\u7b26\u4e32\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u800c\u4e14\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u51fd\u6570\u8bbf\u95ee\u3002<strong>\u8bbf\u95ee\u6587\u6863\u5b57\u7b26\u4e32<\/strong>\uff1aPython\u63d0\u4f9b\u4e86<code>__doc__<\/code>\u5c5e\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u8bbf\u95ee\u5b9a\u4e49\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002<strong>\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u8fdb\u884c\u6ce8\u91ca<\/strong>\uff1a\u5408\u7406\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u4fbf\u4e8e\u56e2\u961f\u534f\u4f5c\u548c\u4ee3\u7801\u7684\u957f\u671f\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u6587\u6863\u5b57\u7b26\u4e32\u662f\u4e00\u4e2a\u7b80\u5355\u4f46\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\u3002\u5728\u7f16\u5199\u51fd\u6570\u3001\u7c7b\u6216\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u5728\u5176\u5f00\u5934\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\u6765\u5305\u542b\u6587\u6863\u5b57\u7b26\u4e32\u3002\u6587\u6863\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u53ef\u4ee5\u5305\u62ec\u63cf\u8ff0\u3001\u53c2\u6570\u8bf4\u660e\u3001\u8fd4\u56de\u503c\u8bf4\u660e\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5728\u51fd\u6570\u4e2d\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u901a\u5e38\u7528\u4e8e\u63cf\u8ff0\u51fd\u6570\u7684\u529f\u80fd\u3001\u53c2\u6570\u548c\u8fd4\u56de\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    a (int, float): \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>    b (int, float): \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>    \u8fd4\u56de:<\/p>\n<p>    int, float: \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u8be6\u7ec6\u63cf\u8ff0\u4e86<code>add<\/code>\u51fd\u6570\u7684\u529f\u80fd\u3001\u53c2\u6570\u7c7b\u578b\u548c\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u6837\uff0c\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5728\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5f88\u5feb\u4e86\u89e3\u5176\u7528\u9014\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u5728\u7c7b\u4e2d\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728\u7c7b\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u7528\u4e8e\u63cf\u8ff0\u7c7b\u7684\u529f\u80fd\u3001\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Dog:<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd9\u4e2a\u7c7b\u8868\u793a\u4e00\u53ea\u72d7\u3002<\/p>\n<p>    \u5c5e\u6027:<\/p>\n<p>    name (str): \u72d7\u7684\u540d\u5b57<\/p>\n<p>    age (int): \u72d7\u7684\u5e74\u9f84<\/p>\n<p>    \u65b9\u6cd5:<\/p>\n<p>    bark(): \u8ba9\u72d7\u5420\u53eb<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def bark(self):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        \u8ba9\u72d7\u5420\u53eb\u3002<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        print(&quot;Woof!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u8be6\u7ec6\u63cf\u8ff0\u4e86<code>Dog<\/code>\u7c7b\u7684\u529f\u80fd\u3001\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u8fd9\u6837\uff0c\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5728\u4f7f\u7528\u8fd9\u4e2a\u7c7b\u65f6\uff0c\u53ef\u4ee5\u5f88\u5feb\u4e86\u89e3\u5176\u7528\u9014\u548c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1.3\u3001\u5728\u6a21\u5757\u4e2d\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728\u6a21\u5757\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u7528\u4e8e\u63cf\u8ff0\u6a21\u5757\u7684\u529f\u80fd\u548c\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&quot;&quot;&quot;<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u5305\u542b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u5b66\u51fd\u6570\u3002<\/p>\n<p>\u51fd\u6570:<\/p>\n<p>add(a, b): \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>subtract(a, b): \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u5dee<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def subtract(a, b):<\/p>\n<p>    return a - b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u8be6\u7ec6\u63cf\u8ff0\u4e86\u6a21\u5757\u7684\u529f\u80fd\u548c\u5305\u542b\u7684\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u5176\u4ed6\u5f00\u53d1\u4eba\u5458\u5728\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u5f88\u5feb\u4e86\u89e3\u5176\u7528\u9014\u548c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbf\u95ee\u6587\u6863\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>__doc__<\/code>\u5c5e\u6027\u6765\u8bbf\u95ee\u5b9a\u4e49\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u548c\u751f\u6210\u81ea\u52a8\u5316\u6587\u6863\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u8bbf\u95ee\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u51fd\u6570\u5bf9\u8c61\u7684<code>__doc__<\/code>\u5c5e\u6027\u6765\u8bbf\u95ee\u5176\u6587\u6863\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>print(add.__doc__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u7684\u548c\u3002<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u8bbf\u95ee\u7c7b\u7684\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7c7b\u5bf9\u8c61\u7684<code>__doc__<\/code>\u5c5e\u6027\u6765\u8bbf\u95ee\u5176\u6587\u6863\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Dog:<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd9\u4e2a\u7c7b\u8868\u793a\u4e00\u53ea\u72d7\u3002<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def __init__(self, name, age):<\/p>\n<p>        self.name = name<\/p>\n<p>        self.age = age<\/p>\n<p>    def bark(self):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        \u8ba9\u72d7\u5420\u53eb\u3002<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        print(&quot;Woof!&quot;)<\/p>\n<p>print(Dog.__doc__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>\u8fd9\u4e2a\u7c7b\u8868\u793a\u4e00\u53ea\u72d7\u3002<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3\u3001\u8bbf\u95ee\u6a21\u5757\u7684\u6587\u6863\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6a21\u5757\u5bf9\u8c61\u7684<code>__doc__<\/code>\u5c5e\u6027\u6765\u8bbf\u95ee\u5176\u6587\u6863\u5b57\u7b26\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&quot;&quot;&quot;<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u5305\u542b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u5b66\u51fd\u6570\u3002<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>print(__doc__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>\u8fd9\u4e2a\u6a21\u5757\u5305\u542b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u5b66\u51fd\u6570\u3002<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u8fdb\u884c\u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u5408\u7406\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u53ef\u4ee5\u6781\u5927\u5730\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\uff0c\u4fbf\u4e8e\u56e2\u961f\u534f\u4f5c\u548c\u4ee3\u7801\u7684\u957f\u671f\u7ef4\u62a4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<\/p>\n<p><h4>3.1\u3001\u7b80\u660e\u627c\u8981<\/h4>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\u5e94\u8be5\u7b80\u660e\u627c\u8981\u5730\u63cf\u8ff0\u51fd\u6570\u3001\u7c7b\u6216\u6a21\u5757\u7684\u529f\u80fd\u3002\u907f\u514d\u5197\u957f\u7684\u63cf\u8ff0\uff0c\u4fdd\u6301\u6e05\u6670\u548c\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u8be6\u7ec6\u8bf4\u660e\u53c2\u6570\u548c\u8fd4\u56de\u503c<\/h4>\n<\/p>\n<p><p>\u5728\u63cf\u8ff0\u51fd\u6570\u65f6\uff0c\u5e94\u8be5\u8be6\u7ec6\u8bf4\u660e\u53c2\u6570\u7684\u7c7b\u578b\u548c\u610f\u4e49\uff0c\u4ee5\u53ca\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u548c\u610f\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    a (int, float): \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>    b (int, float): \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>    \u8fd4\u56de:<\/p>\n<p>    int, float: \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3\u3001\u4f7f\u7528\u5408\u9002\u7684\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u6587\u6863\u7684\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u5408\u9002\u7684\u683c\u5f0f\u548c\u7f29\u8fdb\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u3001\u6bb5\u843d\u548c\u4ee3\u7801\u5757\u6765\u7ec4\u7ec7\u6587\u6863\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    - a (int, float): \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>    - b (int, float): \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>    \u8fd4\u56de:<\/p>\n<p>    - int, float: \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    \u4f8b\u5b50:<\/p>\n<p>    &gt;&gt;&gt; add(2, 3)<\/p>\n<p>    5<\/p>\n<p>    &gt;&gt;&gt; add(2.5, 3.5)<\/p>\n<p>    6.0<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6587\u6863\u5b57\u7b26\u4e32\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\u5728\u591a\u4e2a\u573a\u666f\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u7279\u522b\u662f\u5728\u5f00\u53d1\u5927\u578b\u9879\u76ee\u548c\u56e2\u961f\u534f\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u81ea\u52a8\u751f\u6210\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u8bb8\u591a\u5de5\u5177\u53ef\u4ee5\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u81ea\u52a8\u751f\u6210API\u6587\u6863\u3002\u4f8b\u5982\uff0cSphinx\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u53ef\u4ee5\u89e3\u6790Python\u4ee3\u7801\u4e2d\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u5e76\u751f\u6210HTML\u3001PDF\u7b49\u683c\u5f0f\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">&quot;&quot;&quot;<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5757\u5305\u542b\u4e86\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u5b66\u51fd\u6570\u3002<\/p>\n<p>.. module:: math_functions<\/p>\n<p>   :platform: Unix, Windows<\/p>\n<p>   :synopsis: \u4e00\u4e9b\u7b80\u5355\u7684\u6570\u5b66\u51fd\u6570<\/p>\n<p>.. moduleauthor:: \u4f5c\u8005\u540d &lt;em<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>l@example.com&gt;<\/p>\n<p>\u51fd\u6570:<\/p>\n<p>- add(a, b): \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>- subtract(a, b): \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u5dee<\/p>\n<p>&quot;&quot;&quot;<\/p>\n<p>def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    a (int, float): \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>    b (int, float): \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>    \u8fd4\u56de:<\/p>\n<p>    int, float: \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001<a href=\"https:\/\/docs.pingcode.com\/code-review\" target=\"_blank\">\u4ee3\u7801\u5ba1\u67e5<\/a>\u548c\u56e2\u961f\u534f\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u56e2\u961f\u534f\u4f5c\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u53ef\u4ee5\u5e2e\u52a9\u56e2\u961f\u6210\u5458\u5feb\u901f\u7406\u89e3\u4ee3\u7801\u7684\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u5ba1\u67e5\u548c\u534f\u4f5c\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Calculator:<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u5668\u7c7b\u3002<\/p>\n<p>    \u65b9\u6cd5:<\/p>\n<p>    - add(a, b): \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    - subtract(a, b): \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u5dee<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def add(self, a, b):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>        \u53c2\u6570:<\/p>\n<p>        a (int, float): \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>        b (int, float): \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>        \u8fd4\u56de:<\/p>\n<p>        int, float: \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        return a + b<\/p>\n<p>    def subtract(self, a, b):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u5dee\u3002<\/p>\n<p>        \u53c2\u6570:<\/p>\n<p>        a (int, float): \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>        b (int, float): \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>        \u8fd4\u56de:<\/p>\n<p>        int, float: \u4e24\u4e2a\u6570\u5b57\u7684\u5dee<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        return a - b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3\u3001\u4ee3\u7801\u8c03\u8bd5\u548c\u7ef4\u62a4<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u8c03\u8bd5\u548c\u7ef4\u62a4\u8fc7\u7a0b\u4e2d\uff0c\u6587\u6863\u5b57\u7b26\u4e32\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5feb\u901f\u627e\u5230\u95ee\u9898\u6240\u5728\uff0c\u5e76\u7406\u89e3\u4ee3\u7801\u7684\u903b\u8f91\u548c\u529f\u80fd\uff0c\u4ece\u800c\u63d0\u9ad8\u8c03\u8bd5\u548c\u7ef4\u62a4\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u5546\u3002<\/p>\n<p>    \u53c2\u6570:<\/p>\n<p>    a (int, float): \u88ab\u9664\u6570<\/p>\n<p>    b (int, float): \u9664\u6570<\/p>\n<p>    \u8fd4\u56de:<\/p>\n<p>    float: \u4e24\u4e2a\u6570\u5b57\u7684\u5546<\/p>\n<p>    \u5f02\u5e38:<\/p>\n<p>    ZeroDivisionError: \u5982\u679c\u9664\u6570\u4e3a\u96f6\u5219\u629b\u51fa\u6b64\u5f02\u5e38<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    if b == 0:<\/p>\n<p>        raise ZeroDivisionError(&quot;\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6&quot;)<\/p>\n<p>    return a \/ b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6587\u6863\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u6587\u6863\u5b57\u7b26\u4e32\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u683c\u5f0f\u5316\u5de5\u5177\u548c\u6807\u51c6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u683c\u5f0f\u5316\u5de5\u5177\u548c\u6807\u51c6\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001reStructuredText (reST)<\/h4>\n<\/p>\n<p><p>reStructuredText\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6587\u6863\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u53ef\u4ee5\u4e0eSphinx\u7b49\u6587\u6863\u751f\u6210\u5de5\u5177\u7ed3\u5408\u4f7f\u7528\u3002reST\u683c\u5f0f\u7b80\u6d01\u660e\u4e86\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6587\u6863\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>    :param a: \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>    :type a: int, float<\/p>\n<p>    :param b: \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>    :type b: int, float<\/p>\n<p>    :return: \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    :rtype: int, float<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2\u3001Google\u98ce\u683c<\/h4>\n<\/p>\n<p><p>Google\u98ce\u683c\u7684\u6587\u6863\u5b57\u7b26\u4e32\u683c\u5f0f\u7b80\u6d01\u6613\u8bfb\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u5f00\u53d1\u9879\u76ee\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>    Args:<\/p>\n<p>        a (int, float): \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>        b (int, float): \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>    Returns:<\/p>\n<p>        int, float: \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3\u3001NumPy\/SciPy\u98ce\u683c<\/h4>\n<\/p>\n<p><p>NumPy\/SciPy\u98ce\u683c\u7684\u6587\u6863\u5b57\u7b26\u4e32\u683c\u5f0f\u8be6\u7ec6\u3001\u89c4\u8303\uff0c\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u9886\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    \u8fd4\u56de\u4e24\u4e2a\u6570\u5b57\u7684\u548c\u3002<\/p>\n<p>    Parameters<\/p>\n<p>    ----------<\/p>\n<p>    a : int, float<\/p>\n<p>        \u7b2c\u4e00\u4e2a\u6570\u5b57<\/p>\n<p>    b : int, float<\/p>\n<p>        \u7b2c\u4e8c\u4e2a\u6570\u5b57<\/p>\n<p>    Returns<\/p>\n<p>    -------<\/p>\n<p>    int, float<\/p>\n<p>        \u4e24\u4e2a\u6570\u5b57\u7684\u548c<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6587\u6863\u5b57\u7b26\u4e32\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7279\u6027\uff0c\u7528\u4e8e\u4e3a\u6a21\u5757\u3001\u51fd\u6570\u3001\u7c7b\u548c\u65b9\u6cd5\u7f16\u5199\u8bf4\u660e\u6587\u6863\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\u548c\u534f\u4f5c\u6548\u7387\u3002\u5728\u7f16\u5199\u6587\u6863\u5b57\u7b26\u4e32\u65f6\uff0c\u5e94\u8be5\u7b80\u660e\u627c\u8981\u5730\u63cf\u8ff0\u529f\u80fd\u3001\u8be6\u7ec6\u8bf4\u660e\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u5e76\u4f7f\u7528\u5408\u9002\u7684\u683c\u5f0f\u3002\u5229\u7528\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u548c\u7ef4\u62a4\u4ee3\u7801\uff0c\u4ece\u800c\u63d0\u9ad8\u9879\u76ee\u7684\u6574\u4f53\u8d28\u91cf\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u6587\u6863\u5b57\u7b26\u4e32\u662f\u4ec0\u4e48\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\u662fPython\u4e2d\u7528\u4e8e\u4e3a\u6a21\u5757\u3001\u7c7b\u3001\u65b9\u6cd5\u548c\u51fd\u6570\u63d0\u4f9b\u6587\u6863\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u901a\u5e38\u4f4d\u4e8e\u5b9a\u4e49\u7684\u7b2c\u4e00\u884c\uff0c\u4f7f\u7528\u4e09\u91cd\u5f15\u53f7\uff08&quot;&quot;&quot;\u6216&#39;&#39;&#39;\uff09\u5305\u88f9\u3002\u6587\u6863\u5b57\u7b26\u4e32\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5e2e\u52a9\u5f00\u53d1\u8005\u7406\u89e3\u4ee3\u7801\u7684\u529f\u80fd\u548c\u4f7f\u7528\u65b9\u6cd5\u3002\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\u6216IDE\u7684\u63d0\u793a\u529f\u80fd\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u8fd9\u4e9b\u6587\u6863\u4fe1\u606f\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u7f16\u5199\u6709\u6548\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u64b0\u5199\u6709\u6548\u7684\u6587\u6863\u5b57\u7b26\u4e32\u5e94\u9075\u5faa\u4e00\u5b9a\u7684\u89c4\u8303\u3002\u901a\u5e38\uff0c\u5f00\u59cb\u90e8\u5206\u5e94\u7b80\u6d01\u660e\u4e86\u5730\u63cf\u8ff0\u529f\u80fd\uff0c\u63a5\u7740\u63d0\u4f9b\u53c2\u6570\u7684\u8bf4\u660e\u548c\u8fd4\u56de\u503c\u7684\u89e3\u91ca\u3002\u6b64\u5916\uff0c\u5982\u679c\u51fd\u6570\u629b\u51fa\u5f02\u5e38\uff0c\u4e5f\u5e94\u5728\u6587\u6863\u4e2d\u6ce8\u660e\u3002\u4f7f\u7528\u4e00\u81f4\u7684\u683c\u5f0f\uff08\u5982Google\u98ce\u683c\u6216NumPy\u98ce\u683c\uff09\u53ef\u4ee5\u63d0\u5347\u6587\u6863\u7684\u4e13\u4e1a\u6027\u548c\u6613\u8bfb\u6027\u3002\u786e\u4fdd\u6587\u6863\u5b57\u7b26\u4e32\u7684\u5185\u5bb9\u51c6\u786e\u3001\u7b80\u660e\uff0c\u5e76\u9002\u65f6\u66f4\u65b0\uff0c\u4ee5\u53cd\u6620\u4ee3\u7801\u7684\u6700\u65b0\u72b6\u6001\u3002<\/p>\n<p><strong>\u5982\u4f55\u67e5\u770b\u5df2\u5b9a\u4e49\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u8981\u67e5\u770b\u5df2\u5b9a\u4e49\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u7684<code>help()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u8f93\u5165<code>help(\u51fd\u6570\u540d)<\/code>\u4f1a\u8f93\u51fa\u8be5\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002\u6b64\u5916\uff0c\u5728\u4e00\u4e9bIDE\u4e2d\uff08\u5982PyCharm\u6216VS Code\uff09\uff0c\u5c06\u5149\u6807\u60ac\u505c\u5728\u51fd\u6570\u540d\u79f0\u4e0a\u4e5f\u53ef\u4ee5\u770b\u5230\u76f8\u5e94\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u8fd9\u79cd\u65b9\u5f0f\u6781\u5927\u5730\u65b9\u4fbf\u4e86\u5f00\u53d1\u8005\u5728\u7f16\u5199\u548c\u8c03\u8bd5\u4ee3\u7801\u65f6\u83b7\u53d6\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5b9a\u4e49\u6587\u6863\u5b57\u7b26\u4e32\u3001\u8bbf\u95ee\u6587\u6863\u5b57\u7b26\u4e32\u3001\u4f7f\u7528\u6587\u6863\u5b57\u7b26\u4e32\u8fdb\u884c\u6ce8\u91ca\u3002 \u6587\u6863\u5b57\u7b26\u4e32\uff08 [&hellip;]","protected":false},"author":3,"featured_media":1053867,"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\/1053862"}],"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=1053862"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053862\/revisions"}],"predecessor-version":[{"id":1053868,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053862\/revisions\/1053868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053867"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}