{"id":1165382,"date":"2025-01-15T15:19:45","date_gmt":"2025-01-15T07:19:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1165382.html"},"modified":"2025-01-15T15:19:47","modified_gmt":"2025-01-15T07:19:47","slug":"python%e5%a6%82%e4%bd%95%e6%9f%a5%e8%af%a2%e5%87%bd%e6%95%b0%e7%9a%84%e6%8c%87%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1165382.html","title":{"rendered":"python\u5982\u4f55\u67e5\u8be2\u51fd\u6570\u7684\u6307\u4ee4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25205736\/d3e9849d-5a9b-4b18-a47b-83820f084e7b.webp\" alt=\"python\u5982\u4f55\u67e5\u8be2\u51fd\u6570\u7684\u6307\u4ee4\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u67e5\u8be2\u51fd\u6570\u7684\u6307\u4ee4<\/strong>\u3001<strong>\u4f7f\u7528\u5185\u7f6e\u7684\u5e2e\u52a9\u7cfb\u7edf<\/strong>\u3001<strong>\u67e5\u770b\u51fd\u6570\u7684\u6e90\u4ee3\u7801<\/strong>\u3001<strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u7b49<\/strong>\u3002\u6700\u5e38\u7528\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5185\u7f6e<code>help()<\/code>\u51fd\u6570\uff0c\u5b83\u4f1a\u63d0\u4f9b\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\uff0c\u5176\u4e2d\u901a\u5e38\u5305\u542b\u51fd\u6570\u7684\u7528\u6cd5\u548c\u53c2\u6570\u8bf4\u660e\u3002\u4e0b\u9762\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u7684\u5e2e\u52a9\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e<code>help()<\/code>\u51fd\u6570\u662f\u67e5\u8be2\u51fd\u6570\u6307\u4ee4\u548c\u6587\u6863\u5b57\u7b26\u4e32\u7684\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5b83\u4f1a\u663e\u793a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u51fd\u6570\u662f\u7528Python\u7f16\u5199\u7684\uff0c\u8fd8\u4f1a\u663e\u793a\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function(param1, param2):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example function.<\/p>\n<p>    Parameters:<\/p>\n<p>    param1 (int): The first parameter.<\/p>\n<p>    param2 (int): The second parameter.<\/p>\n<p>    Returns:<\/p>\n<p>    int: The sum of param1 and param2.<\/p>\n<p>    &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><strong>\u4f7f\u7528dir()\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><code>dir()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5bf9\u8c61\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5217\u8868\u3002\u867d\u7136\u5b83\u4e0d\u4f1a\u663e\u793a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4e86\u89e3\u51fd\u6570\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\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><strong>\u67e5\u770b\u51fd\u6570\u7684\u6e90\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8ePython\u5185\u7f6e\u51fd\u6570\u6216\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u67e5\u770b\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u6709\u7528\u7684\u51fd\u6570\uff0c\u4f8b\u5982<code>getsource()<\/code>\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>print(inspect.getsource(example_function))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u65b9\u4fbf\u5730\u67e5\u8be2\u51fd\u6570\u7684\u6307\u4ee4\u548c\u6587\u6863\u3002\u4f8b\u5982\uff0cIPython\u548cJupyter Notebook\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u5e2e\u52a9\u7cfb\u7edf\uff0c\u53ea\u9700\u5728\u51fd\u6570\u540d\u540e\u52a0\u4e00\u4e2a\u95ee\u53f7\u5373\u53ef\u67e5\u770b\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">example_function?<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684\u5e2e\u52a9\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e<code>help()<\/code>\u51fd\u6570\u662f\u67e5\u8be2\u51fd\u6570\u6307\u4ee4\u548c\u6587\u6863\u5b57\u7b26\u4e32\u7684\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u3002\u5b83\u4f1a\u663e\u793a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u51fd\u6570\u662f\u7528Python\u7f16\u5199\u7684\uff0c\u8fd8\u4f1a\u663e\u793a\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p><code>help()<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u5728Python\u7684\u4ea4\u4e92\u5f0f\u89e3\u91ca\u5668\u4e2d\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u5728\u811a\u672c\u4e2d\u8c03\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function(param1, param2):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example function.<\/p>\n<p>    Parameters:<\/p>\n<p>    param1 (int): The first parameter.<\/p>\n<p>    param2 (int): The second parameter.<\/p>\n<p>    Returns:<\/p>\n<p>    int: The sum of param1 and param2.<\/p>\n<p>    &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>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code>Help on function example_function in module __m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__:<\/p>\n<p>example_function(param1, param2)<\/p>\n<p>    This is an example function.<\/p>\n<p>    Parameters:<\/p>\n<p>    param1 (int): The first parameter.<\/p>\n<p>    param2 (int): The second parameter.<\/p>\n<p>    Returns:<\/p>\n<p>    int: The sum of param1 and param2.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528help()\u67e5\u770b\u6a21\u5757\u548c\u7c7b<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u51fd\u6570\uff0c<code>help()<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u7528\u4e8e\u67e5\u770b\u6a21\u5757\u548c\u7c7b\u7684\u6587\u6863\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>help(math)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f1a\u8f93\u51famath\u6a21\u5757\u7684\u8be6\u7ec6\u6587\u6863\uff0c\u5305\u62ec\u6240\u6709\u7684\u51fd\u6570\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u5bf9\u4e8e\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ExampleClass:<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example class.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def method(self):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        This is an example method.<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        pass<\/p>\n<p>help(ExampleClass)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51faExampleClass\u7c7b\u7684\u8be6\u7ec6\u6587\u6863\uff0c\u5305\u62ec\u7c7b\u7684docstring\u548c\u6240\u6709\u65b9\u6cd5\u7684docstring\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528dir()\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>dir()<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5bf9\u8c61\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5217\u8868\u3002\u867d\u7136\u5b83\u4e0d\u4f1a\u663e\u793a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u4f46\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u4e86\u89e3\u51fd\u6570\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>dir()<\/code>\u67e5\u770b\u51fd\u6570\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(dir(example_function))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>[&#39;__annotations__&#39;, &#39;__call__&#39;, &#39;__class__&#39;, &#39;__closure__&#39;, &#39;__code__&#39;, &#39;__defaults__&#39;, &#39;__delattr__&#39;, &#39;__dict__&#39;, &#39;__dir__&#39;, &#39;__doc__&#39;, &#39;__eq__&#39;, &#39;__format__&#39;, &#39;__ge__&#39;, &#39;__get__&#39;, &#39;__getattribute__&#39;, &#39;__globals__&#39;, &#39;__gt__&#39;, &#39;__hash__&#39;, &#39;__init__&#39;, &#39;__init_subclass__&#39;, &#39;__kwdefaults__&#39;, &#39;__le__&#39;, &#39;__lt__&#39;, &#39;__module__&#39;, &#39;__name__&#39;, &#39;__ne__&#39;, &#39;__new__&#39;, &#39;__qualname__&#39;, &#39;__reduce__&#39;, &#39;__reduce_ex__&#39;, &#39;__repr__&#39;, &#39;__setattr__&#39;, &#39;__sizeof__&#39;, &#39;__str__&#39;, &#39;__subclasshook__&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528dir()\u67e5\u770b\u6a21\u5757\u548c\u7c7b<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c<code>dir()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u67e5\u770b\u6a21\u5757\u548c\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\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>\u4f1a\u8f93\u51famath\u6a21\u5757\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ExampleClass:<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example class.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def method(self):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        This is an example method.<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        pass<\/p>\n<p>print(dir(ExampleClass))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51faExampleClass\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u67e5\u770b\u51fd\u6570\u7684\u6e90\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8ePython\u5185\u7f6e\u51fd\u6570\u6216\u6807\u51c6\u5e93\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u67e5\u770b\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u4e2a\u6709\u7528\u7684\u51fd\u6570\uff0c\u4f8b\u5982<code>getsource()<\/code>\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528inspect\u6a21\u5757\u67e5\u770b\u6e90\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def example_function(param1, param2):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example function.<\/p>\n<p>    Parameters:<\/p>\n<p>    param1 (int): The first parameter.<\/p>\n<p>    param2 (int): The second parameter.<\/p>\n<p>    Returns:<\/p>\n<p>    int: The sum of param1 and param2.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return param1 + param2<\/p>\n<p>print(inspect.getsource(example_function))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\uff0c\u4f1a\u8f93\u51faexample_function\u51fd\u6570\u7684\u6e90\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code>def example_function(param1, param2):<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example function.<\/p>\n<p>    Parameters:<\/p>\n<p>    param1 (int): The first parameter.<\/p>\n<p>    param2 (int): The second parameter.<\/p>\n<p>    Returns:<\/p>\n<p>    int: The sum of param1 and param2.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    return param1 + param2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528inspect\u6a21\u5757\u67e5\u770b\u7c7b\u548c\u6a21\u5757\u7684\u6e90\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c<code>inspect<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u4e8e\u67e5\u770b\u7c7b\u548c\u6a21\u5757\u7684\u6e90\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ExampleClass:<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example class.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def method(self):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        This is an example method.<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        pass<\/p>\n<p>print(inspect.getsource(ExampleClass))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51faExampleClass\u7c7b\u7684\u6e90\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code>class ExampleClass:<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    This is an example class.<\/p>\n<p>    &quot;&quot;&quot;<\/p>\n<p>    def method(self):<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        This is an example method.<\/p>\n<p>        &quot;&quot;&quot;<\/p>\n<p>        pass<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>print(inspect.getsource(math))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u6ce8\u610f\uff0c<code>inspect.getsource()<\/code>\u53ea\u80fd\u7528\u4e8ePython\u6e90\u4ee3\u7801\u6587\u4ef6\uff0c\u5bf9\u4e8e\u5185\u7f6e\u6a21\u5757\uff08\u5982math\u6a21\u5757\uff09\uff0c\u5b83\u4f1a\u629b\u51fa<code>TypeError<\/code>\u5f02\u5e38\uff0c\u56e0\u4e3a\u5185\u7f6e\u6a21\u5757\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u65e0\u6cd5\u76f4\u63a5\u67e5\u770b\u5176\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177<\/h3>\n<\/p>\n<p><p>\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u65b9\u4fbf\u5730\u67e5\u8be2\u51fd\u6570\u7684\u6307\u4ee4\u548c\u6587\u6863\u3002\u4f8b\u5982\uff0cIPython\u548cJupyter Notebook\u63d0\u4f9b\u4e86\u975e\u5e38\u5f3a\u5927\u7684\u5e2e\u52a9\u7cfb\u7edf\uff0c\u53ea\u9700\u5728\u51fd\u6570\u540d\u540e\u52a0\u4e00\u4e2a\u95ee\u53f7\u5373\u53ef\u67e5\u770b\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528IPython\u548cJupyter Notebook<\/h4>\n<\/p>\n<p><p>\u5728IPython\u6216Jupyter Notebook\u4e2d\uff0c\u53ea\u9700\u5728\u51fd\u6570\u540d\u540e\u52a0\u4e00\u4e2a\u95ee\u53f7\u5373\u53ef\u67e5\u770b\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u6e90\u4ee3\u7801\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">example_function?<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51faexample_function\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u5bf9\u4e8e\u7c7b\u548c\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ExampleClass?<\/p>\n<p>math?<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51fa\u76f8\u5e94\u7c7b\u548c\u6a21\u5757\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u6e90\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528pydoc\u6a21\u5757<\/h4>\n<\/p>\n<p><p>Python\u7684<code>pydoc<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8e<code>help()<\/code>\u51fd\u6570\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u751f\u6210\u6a21\u5757\u3001\u7c7b\u548c\u51fd\u6570\u7684\u6587\u6863\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u6216\u811a\u672c\u4f7f\u7528<code>pydoc<\/code>\u6a21\u5757\u3002\u4f8b\u5982\uff0c\u5728\u547d\u4ee4\u884c\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code>pydoc example_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51faexample_function\u51fd\u6570\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><p>\u5728\u811a\u672c\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pydoc<\/p>\n<p>print(pydoc.render_doc(example_function))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f1a\u8f93\u51faexample_function\u51fd\u6570\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u67e5\u8be2\u51fd\u6570\u6307\u4ee4\u548c\u6587\u6863\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\u3001<code>dir()<\/code>\u51fd\u6570\u3001<code>inspect<\/code>\u6a21\u5757\u548c\u7b2c\u4e09\u65b9\u5de5\u5177\uff08\u5982IPython\u548cJupyter Notebook\uff09\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570<\/strong>\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u663e\u793a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\u548c\u6e90\u4ee3\u7801\uff0c\u975e\u5e38\u9002\u5408\u5feb\u901f\u67e5\u770b\u51fd\u6570\u7684\u7528\u6cd5\u548c\u53c2\u6570\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570<\/strong>\u53ef\u4ee5\u67e5\u770b\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f46\u4e0d\u4f1a\u663e\u793a\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u9002\u5408\u4e86\u89e3\u5bf9\u8c61\u7684\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757<\/strong>\u53ef\u4ee5\u67e5\u770b\u51fd\u6570\u3001\u7c7b\u548c\u6a21\u5757\u7684\u6e90\u4ee3\u7801\uff0c\u975e\u5e38\u9002\u5408\u6df1\u5165\u4e86\u89e3\u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177<\/strong>\uff08\u5982IPython\u548cJupyter Notebook\uff09\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u67e5\u8be2\u51fd\u6570\u7684\u6587\u6863\u548c\u6e90\u4ee3\u7801\uff0c\u9002\u5408\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u67e5\u8be2Python\u51fd\u6570\u7684\u6307\u4ee4\u548c\u6587\u6863\uff0c\u63d0\u9ad8\u4ee3\u7801\u9605\u8bfb\u548c\u8c03\u8bd5\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u6216\u5b9e\u73b0\u7ec6\u8282\uff1f<\/strong><br \/>\u8981\u67e5\u770bPython\u51fd\u6570\u7684\u6e90\u4ee3\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u4e2d\u7684<code>getsource<\/code>\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5bfc\u5165\u4e86\u8be5\u6a21\u5757\u3002\u63a5\u4e0b\u6765\uff0c\u8c03\u7528<code>inspect.getsource(\u51fd\u6570\u540d)<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3002\u5982\u679c\u51fd\u6570\u662f\u5185\u7f6e\u7684\uff0c\u53ef\u80fd\u65e0\u6cd5\u83b7\u53d6\u6e90\u4ee3\u7801\uff0c\u60a8\u53ef\u4ee5\u67e5\u9605\u76f8\u5173\u6587\u6863\u6216\u4f7f\u7528<code>help(\u51fd\u6570\u540d)<\/code>\u67e5\u770b\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6Python\u51fd\u6570\u7684\u53c2\u6570\u4fe1\u606f\uff1f<\/strong><br \/>\u4f7f\u7528<code>inspect.signature<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u51fd\u6570\u7684\u53c2\u6570\u4fe1\u606f\u3002\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a<code>Signature<\/code>\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u51fd\u6570\u7684\u53c2\u6570\u5217\u8868\u53ca\u5176\u9ed8\u8ba4\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import inspect\n\ndef example_function(a, b=10):\n    return a + b\n\nsig = inspect.signature(example_function)\nprint(sig)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8f93\u51fa<code>(a, b=10)<\/code>\uff0c\u663e\u793a\u51fd\u6570\u7684\u53c2\u6570\u53ca\u5176\u9ed8\u8ba4\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u83b7\u53d6Python\u51fd\u6570\u7684\u6587\u6863\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u6bcf\u4e2aPython\u51fd\u6570\u901a\u5e38\u90fd\u5305\u542b\u6587\u6863\u5b57\u7b26\u4e32\uff08docstring\uff09\uff0c\u7528\u4e8e\u63cf\u8ff0\u5176\u529f\u80fd\u548c\u7528\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7<code>__doc__<\/code>\u5c5e\u6027\u8bbf\u95ee\u6587\u6863\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>print(\u51fd\u6570\u540d.__doc__)<\/code>\u53ef\u4ee5\u67e5\u770b\u8be5\u51fd\u6570\u7684\u8bf4\u660e\u3002\u5982\u679c\u8be5\u51fd\u6570\u6ca1\u6709\u6587\u6863\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de\u503c\u4e3a<code>None<\/code>\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u51fd\u6570\u7684\u7528\u9014\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u67e5\u8be2\u51fd\u6570\u7684\u6307\u4ee4\u3001\u4f7f\u7528\u5185\u7f6e\u7684\u5e2e\u52a9\u7cfb\u7edf\u3001\u67e5\u770b\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5de5\u5177\u7b49\u3002\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1165387,"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\/1165382"}],"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=1165382"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165382\/revisions"}],"predecessor-version":[{"id":1165389,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1165382\/revisions\/1165389"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1165387"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1165382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1165382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1165382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}