{"id":1151994,"date":"2025-01-13T17:20:09","date_gmt":"2025-01-13T09:20:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151994.html"},"modified":"2025-01-13T17:20:12","modified_gmt":"2025-01-13T09:20:12","slug":"python%e5%a6%82%e4%bd%95%e7%9c%8b%e6%9c%89%e5%93%aa%e4%ba%9b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151994.html","title":{"rendered":"python\u5982\u4f55\u770b\u6709\u54ea\u4e9b\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182108\/94f6b05b-6d08-4287-ab79-389bbd2ab73d.webp\" alt=\"python\u5982\u4f55\u770b\u6709\u54ea\u4e9b\u65b9\u6cd5\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecdir()\u3001help()\u548cinspect\u6a21\u5757\u3002\u4f7f\u7528dir()\u51fd\u6570\u53ef\u4ee5\u5217\u51fa\u4e00\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f7f\u7528help()\u51fd\u6570\u53ef\u4ee5\u67e5\u770b\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u4f7f\u7528inspect\u6a21\u5757\u53ef\u4ee5\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u5bf9\u8c61\u4fe1\u606f\u3002<\/strong>\u5176\u4e2d\uff0cdir()\u51fd\u6570\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u76f4\u63a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528dir()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>dir()\u51fd\u6570\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u5217\u51fa\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5b83\u6700\u5e38\u7528\u7684\u5f62\u5f0f\u662f<code>dir(object)<\/code>\uff0c\u5176\u4e2dobject\u662f\u4f60\u60f3\u67e5\u770b\u7684\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5<\/p>\n<p>methods = dir(str)<\/p>\n<p>print(methods)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>dir()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u4e86\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u9002\u7528\u4e8e\u5185\u7f6e\u5bf9\u8c61\uff0c\u8fd8\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u7c7b\u548c\u6a21\u5757\u3002<strong>dir()\u51fd\u6570\u7684\u4e00\u4e2a\u4f18\u52bf\u662f\u5b83\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5feb\u901f\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528help()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>help()\u51fd\u6570\u4e5f\u662fPython\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u67e5\u770b\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u8bf4\u660e\u3002\u5b83\u6700\u5e38\u7528\u7684\u5f62\u5f0f\u662f<code>help(object)<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f<\/p>\n<p>help(str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>help()\u51fd\u6570\u4f1a\u8f93\u51fa\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u6bcf\u4e2a\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u7528\u9014\u53ca\u4f7f\u7528\u793a\u4f8b\u3002\u5b83\u9002\u5408\u9700\u8981\u6df1\u5165\u4e86\u89e3\u5bf9\u8c61\u7ec6\u8282\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528inspect\u6a21\u5757<\/h3>\n<\/p>\n<p><p>inspect\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u5e38\u7528\u7684\u51fd\u6570\u5305\u62ec<code>inspect.getmembers()<\/code>\u548c<code>inspect.isfunction()<\/code>\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458<\/strong><\/h2>\n<p>members = inspect.getmembers(str)<\/p>\n<p>print(members)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>inspect\u6a21\u5757\u9002\u5408\u9700\u8981\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u5bf9\u8c61\u4fe1\u606f\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u67e5\u770b\u5bf9\u8c61\u7684\u6e90\u4ee3\u7801\u3001\u83b7\u53d6\u5bf9\u8c61\u7684\u53c2\u6570\u7b49\u3002<strong>inspect\u6a21\u5757\u7684\u4f18\u52bf\u5728\u4e8e\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7ec6\u8282\u4fe1\u606f\uff0c\u9002\u5408\u9ad8\u7ea7\u7528\u6237\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u56db\u3001\u793a\u4f8b\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7c7b\uff0c\u5e76\u4e14\u60f3\u67e5\u770b\u5b83\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = 10<\/p>\n<p>    def my_method(self):<\/p>\n<p>        return self.value<\/p>\n<h2><strong>\u4f7f\u7528dir()\u51fd\u6570\u67e5\u770b\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h2>\n<p>print(dir(MyClass))<\/p>\n<h2><strong>\u4f7f\u7528help()\u51fd\u6570\u67e5\u770b\u7c7b\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>help(MyClass)<\/p>\n<h2><strong>\u4f7f\u7528inspect\u6a21\u5757\u67e5\u770b\u7c7b\u7684\u6240\u6709\u6210\u5458<\/strong><\/h2>\n<p>print(inspect.getmembers(MyClass))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230\u5982\u4f55\u4f7f\u7528\u4e0d\u540c\u7684\u5de5\u5177\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e2a\u65b9\u6cd5\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001dir()\u51fd\u6570\u7684\u8be6\u7ec6\u5e94\u7528<\/h3>\n<\/p>\n<p><p><strong>dir()\u51fd\u6570\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\uff0c\u7528\u4e8e\u5feb\u901f\u67e5\u770b\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1\u3001\u67e5\u770b\u5185\u7f6e\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5185\u7f6e\u5bf9\u8c61\u662fPython\u8bed\u8a00\u81ea\u5e26\u7684\u5bf9\u8c61\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u4f7f\u7528dir()\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u67e5\u770b\u8fd9\u4e9b\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u5217\u8868\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5<\/p>\n<p>methods = dir(list)<\/p>\n<p>print(methods)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8f93\u51fa\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u770b\u5230\u5217\u8868\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5\uff0c\u4f8b\u5982append()\u3001extend()\u3001insert()\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u81ea\u5b9a\u4e49\u7c7b\uff0cdir()\u51fd\u6570\u540c\u6837\u9002\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = 10<\/p>\n<p>    def my_method(self):<\/p>\n<p>        return self.value<\/p>\n<h2><strong>\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h2>\n<p>methods = dir(MyClass)<\/p>\n<p>print(methods)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7684\u7ed3\u679c\u4f1a\u5305\u542b\u81ea\u5b9a\u4e49\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4ee5\u53ca\u4e00\u4e9bPython\u9ed8\u8ba4\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u4f8b\u5982__init__()\u3001<strong>str<\/strong>()\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001help()\u51fd\u6570\u7684\u8be6\u7ec6\u5e94\u7528<\/h3>\n<\/p>\n<p><p><strong>help()\u51fd\u6570\u7528\u4e8e\u67e5\u770b\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u6bcf\u4e2a\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u8bf4\u660e\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1\u3001\u67e5\u770b\u5185\u7f6e\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u5185\u7f6e\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\u53ef\u4ee5\u901a\u8fc7help()\u51fd\u6570\u6765\u67e5\u770b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f<\/p>\n<p>help(str)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7684\u7ed3\u679c\u4f1a\u5305\u542b\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u8bf4\u660e\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u65b9\u6cd5\u7684\u4f7f\u7528\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u67e5\u770b\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6a21\u5757\uff0chelp()\u51fd\u6570\u540c\u6837\u9002\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u67e5\u770bmath\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>help(math)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7684\u7ed3\u679c\u4f1a\u5305\u542bmath\u6a21\u5757\u7684\u6240\u6709\u51fd\u6570\u548c\u5e38\u91cf\u7684\u8bf4\u660e\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u51fd\u6570\u7684\u4f7f\u7528\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001inspect\u6a21\u5757\u7684\u8be6\u7ec6\u5e94\u7528<\/h3>\n<\/p>\n<p><p><strong>inspect\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\uff0c\u7528\u4e8e\u83b7\u53d6\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u9002\u5408\u9700\u8981\u83b7\u53d6\u66f4\u591a\u7ec6\u8282\u7684\u60c5\u51b5\u3002<\/strong><\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528inspect.getmembers()\u51fd\u6570\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<h2><strong>\u83b7\u53d6\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458<\/strong><\/h2>\n<p>members = inspect.getmembers(str)<\/p>\n<p>print(members)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7684\u7ed3\u679c\u4f1a\u5305\u542b\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458\uff0c\u5305\u62ec\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u4e3a\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528inspect.isfunction()\u51fd\u6570\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u4e3a\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def my_function():<\/p>\n<p>    pass<\/p>\n<h2><strong>\u68c0\u67e5\u5bf9\u8c61\u662f\u5426\u4e3a\u51fd\u6570<\/strong><\/h2>\n<p>is_func = inspect.isfunction(my_function)<\/p>\n<p>print(is_func)  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528dir()\u3001help()\u548cinspect\u6a21\u5757\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = 10<\/p>\n<p>    def my_method(self):<\/p>\n<p>        return self.value<\/p>\n<h2><strong>\u4f7f\u7528dir()\u51fd\u6570\u67e5\u770b\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h2>\n<p>print(&quot;dir()\u51fd\u6570\u7ed3\u679c:&quot;)<\/p>\n<p>print(dir(MyClass))<\/p>\n<h2><strong>\u4f7f\u7528help()\u51fd\u6570\u67e5\u770b\u7c7b\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>print(&quot;\\nhelp()\u51fd\u6570\u7ed3\u679c:&quot;)<\/p>\n<p>help(MyClass)<\/p>\n<h2><strong>\u4f7f\u7528inspect\u6a21\u5757\u67e5\u770b\u7c7b\u7684\u6240\u6709\u6210\u5458<\/strong><\/h2>\n<p>print(&quot;\\ninspect\u6a21\u5757\u7ed3\u679c:&quot;)<\/p>\n<p>members = inspect.getmembers(MyClass)<\/p>\n<p>print(members)<\/p>\n<h2><strong>\u4f7f\u7528inspect.isfunction()\u68c0\u67e5\u7c7b\u7684\u65b9\u6cd5<\/strong><\/h2>\n<p>print(&quot;\\n\u68c0\u67e5\u7c7b\u7684\u65b9\u6cd5\u662f\u5426\u4e3a\u51fd\u6570:&quot;)<\/p>\n<p>for name, member in members:<\/p>\n<p>    if inspect.isfunction(member):<\/p>\n<p>        print(f&quot;{name} \u662f\u51fd\u6570&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u53ef\u4ee5\u6e05\u695a\u5730\u770b\u5230\u5982\u4f55\u7ed3\u5408\u4f7f\u7528\u4e0d\u540c\u7684\u5de5\u5177\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<strong>\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u4f18\u52bf\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5176\u4ed6\u67e5\u770b\u5bf9\u8c61\u65b9\u6cd5\u7684\u5de5\u5177\u548c\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u9664\u4e86dir()\u3001help()\u548cinspect\u6a21\u5757\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u5de5\u5177\u548c\u6280\u5de7\u53ef\u4ee5\u7528\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528__dict__\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u5bf9\u8c61\u7684__dict__\u5c5e\u6027\u662f\u4e00\u4e2a\u5b57\u5178\uff0c\u5305\u542b\u4e86\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = 10<\/p>\n<h2><strong>\u67e5\u770b\u5bf9\u8c61\u7684__dict__\u5c5e\u6027<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p>print(obj.__dict__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>__dict__\u5c5e\u6027\u9002\u7528\u4e8e\u67e5\u770b\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528pydoc\u6a21\u5757<\/h4>\n<\/p>\n<p><p>pydoc\u6a21\u5757\u53ef\u4ee5\u751f\u6210Python\u5bf9\u8c61\u7684\u6587\u6863\uff0c\u7c7b\u4f3c\u4e8ehelp()\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pydoc<\/p>\n<h2><strong>\u751f\u6210\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u6587\u6863<\/strong><\/h2>\n<p>doc = pydoc.render_doc(str)<\/p>\n<p>print(doc)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pydoc\u6a21\u5757\u9002\u7528\u4e8e\u751f\u6210\u5bf9\u8c61\u7684\u6587\u6863\uff0c\u4fbf\u4e8e\u67e5\u770b\u548c\u5206\u4eab\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528Jupyter Notebook<\/h4>\n<\/p>\n<p><p>\u5728Jupyter Notebook\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5bf9\u8c61\u540d\u540e\u52a0\u4e0a\u95ee\u53f7\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u5b57\u7b26\u4e32\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f<\/p>\n<p>str?<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Jupyter Notebook\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u4ea4\u4e92\u5f0f\u73af\u5883\uff0c\u9002\u5408\u5feb\u901f\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u67e5\u770b\u7b2c\u4e09\u65b9\u5e93\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u5de5\u5177\u6765\u67e5\u770b\u7b2c\u4e09\u65b9\u5e93\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u67e5\u770b\u7b2c\u4e09\u65b9\u5e93\u7684\u6a21\u5757\u548c\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u67e5\u770bNumPy\u5e93\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528dir()\u51fd\u6570\u67e5\u770bNumPy\u6a21\u5757\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h2>\n<p>print(dir(np))<\/p>\n<h2><strong>\u4f7f\u7528help()\u51fd\u6570\u67e5\u770bNumPy\u6a21\u5757\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>help(np)<\/p>\n<h2><strong>\u4f7f\u7528inspect\u6a21\u5757\u67e5\u770bNumPy\u6a21\u5757\u7684\u6240\u6709\u6210\u5458<\/strong><\/h2>\n<p>print(inspect.getmembers(np))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u67e5\u770b\u7b2c\u4e09\u65b9\u5e93\u7684\u7c7b\u548c\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u67e5\u770bNumPy\u6570\u7ec4\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aNumPy\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3])<\/p>\n<h2><strong>\u4f7f\u7528dir()\u51fd\u6570\u67e5\u770bNumPy\u6570\u7ec4\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/strong><\/h2>\n<p>print(dir(arr))<\/p>\n<h2><strong>\u4f7f\u7528help()\u51fd\u6570\u67e5\u770bNumPy\u6570\u7ec4\u5bf9\u8c61\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>help(arr)<\/p>\n<h2><strong>\u4f7f\u7528inspect\u6a21\u5757\u67e5\u770bNumPy\u6570\u7ec4\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458<\/strong><\/h2>\n<p>print(inspect.getmembers(arr))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u67e5\u770b\u7b2c\u4e09\u65b9\u5e93\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u7c7b\u548c\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u5de5\u5177\u6765\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u7c7b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyClass:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.value = 10<\/p>\n<p>    def my_method(self):<\/p>\n<p>        return self.value<\/p>\n<h2><strong>\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u7684\u5b9e\u4f8b<\/strong><\/h2>\n<p>obj = MyClass()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528dir()\u3001help()\u548cinspect\u6a21\u5757\u6765\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528dir()\u51fd\u6570\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/p>\n<p>print(dir(MyClass))<\/p>\n<h2><strong>\u4f7f\u7528help()\u51fd\u6570\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u7684\u8be6\u7ec6\u4fe1\u606f<\/strong><\/h2>\n<p>help(MyClass)<\/p>\n<h2><strong>\u4f7f\u7528inspect\u6a21\u5757\u67e5\u770b\u81ea\u5b9a\u4e49\u7c7b\u7684\u6240\u6709\u6210\u5458<\/strong><\/h2>\n<p>print(inspect.getmembers(MyClass))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8c03\u8bd5\u548c\u4f18\u5316\u81ea\u5b9a\u4e49\u7c7b\u548c\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5305\u62ecdir()\u3001help()\u3001inspect\u6a21\u5757\u3001__dict__\u5c5e\u6027\u3001pydoc\u6a21\u5757\u548cJupyter Notebook\u3002<strong>\u8fd9\u4e9b\u5de5\u5177\u5404\u6709\u4f18\u52bf\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f<\/strong>\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u53ef\u4ee5\u638c\u63e1\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u5de5\u5177\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u8bfb\u8005\u6709\u6240\u5e2e\u52a9\uff0c\u63d0\u9ad8Python\u5f00\u53d1\u7684\u6548\u7387\u548c\u8d28\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u67e5\u770b\u4e00\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u53ef\u7528\u65b9\u6cd5\u548c\u5c5e\u6027\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dir()<\/code>\u51fd\u6570\u6765\u67e5\u770b\u4e00\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u53ef\u7528\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u53ea\u9700\u5c06\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9<code>dir()<\/code>\uff0c\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u5bf9\u8c61\u5c5e\u6027\u548c\u65b9\u6cd5\u540d\u79f0\u7684\u5217\u8868\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32\u5bf9\u8c61<code>my_str<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>dir(my_str)<\/code>\u6765\u83b7\u53d6\u8be5\u5b57\u7b26\u4e32\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u83b7\u53d6\u67d0\u4e2a\u65b9\u6cd5\u7684\u8be6\u7ec6\u4fe1\u606f\u6216\u6587\u6863\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>help()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7279\u5b9a\u65b9\u6cd5\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u8c03\u7528<code>help()<\/code>\u5e76\u5c06\u65b9\u6cd5\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u53ef\u4ee5\u67e5\u770b\u8be5\u65b9\u6cd5\u7684\u6587\u6863\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>help(str.lower)<\/code>\u5c06\u63d0\u4f9b\u5173\u4e8e\u5b57\u7b26\u4e32<code>lower<\/code>\u65b9\u6cd5\u7684\u8be6\u7ec6\u8bf4\u660e\uff0c\u5305\u62ec\u5b83\u7684\u7528\u9014\u548c\u53c2\u6570\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u63a2\u7d22\u5e93\u4e2d\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff1f<\/strong><br \/>\u9664\u4e86\u4f7f\u7528<code>dir()<\/code>\u548c<code>help()<\/code>\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u6216\u4ee3\u7801\u7f16\u8f91\u5668\u7684\u81ea\u52a8\u8865\u5168\u529f\u80fd\u3002\u8fd9\u4e9b\u5de5\u5177\u901a\u5e38\u4f1a\u5728\u4f60\u8f93\u5165\u5bf9\u8c61\u540d\u540e\u81ea\u52a8\u5217\u51fa\u8be5\u5bf9\u8c61\u7684\u53ef\u7528\u65b9\u6cd5\u3002\u6b64\u5916\uff0c\u50cfJupyter Notebook\u8fd9\u6837\u7684\u4ea4\u4e92\u5f0f\u73af\u5883\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u6863\u548c\u63a2\u7d22\u529f\u80fd\uff0c\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u548c\u6a21\u5757\u6765\u67e5\u770b\u5bf9\u8c61\u7684\u6240\u6709\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ecdir()\u3001help() [&hellip;]","protected":false},"author":3,"featured_media":1152001,"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\/1151994"}],"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=1151994"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151994\/revisions"}],"predecessor-version":[{"id":1152002,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151994\/revisions\/1152002"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152001"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}