{"id":1076623,"date":"2025-01-08T11:55:01","date_gmt":"2025-01-08T03:55:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076623.html"},"modified":"2025-01-08T11:55:04","modified_gmt":"2025-01-08T03:55:04","slug":"python%e5%a6%82%e4%bd%95%e8%87%aa%e5%8a%a8%e5%b8%a6%e5%87%ba%e5%8c%85%e4%b8%ad%e7%9a%84%e5%87%bd%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076623.html","title":{"rendered":"python\u5982\u4f55\u81ea\u52a8\u5e26\u51fa\u5305\u4e2d\u7684\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181115\/5207144a-9e4f-4e46-81d5-16d97d919d23.webp\" alt=\"python\u5982\u4f55\u81ea\u52a8\u5e26\u51fa\u5305\u4e2d\u7684\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528<code>dir()<\/code>\u548c<code>getattr()<\/code>\u81ea\u52a8\u5e26\u51fa\u5305\u4e2d\u7684\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7<code>dir()<\/code>\u51fd\u6570\u5217\u51fa\u6a21\u5757\u6216\u5305\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u7136\u540e\u7ed3\u5408<code>getattr()<\/code>\u51fd\u6570\u83b7\u53d6\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5f15\u7528\u3002<strong>\u901a\u8fc7\u8fd9\u4e24\u4e2a\u51fd\u6570\u53ef\u4ee5\u81ea\u52a8\u5316\u5730\u83b7\u53d6\u5305\u4e2d\u7684\u6240\u6709\u51fd\u6570<\/strong>\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\u6765\u5217\u51fa\u5e76\u8c03\u7528\u4e00\u4e2a\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6a21\u5757\u5e76\u83b7\u53d6\u6240\u6709\u51fd\u6570\u540d\u79f0<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u76ee\u6807\u6a21\u5757\u6216\u5305\u3002\u7136\u540e\u53ef\u4ee5\u4f7f\u7528<code>dir()<\/code>\u51fd\u6570\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e76\u8fc7\u6ee4\u51fa\u51fd\u6570\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import example_module  # \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3aexample_module\u7684\u6a21\u5757<\/p>\n<p>def get_module_functions(module):<\/p>\n<p>    functions = []<\/p>\n<p>    for item in dir(module):<\/p>\n<p>        if callable(getattr(module, item)):<\/p>\n<p>            functions.append(item)<\/p>\n<p>    return functions<\/p>\n<p>functions = get_module_functions(example_module)<\/p>\n<p>print(functions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>getattr()<\/code>\u83b7\u53d6\u51fd\u6570\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>getattr()<\/code>\u53ef\u4ee5\u4ece\u6a21\u5757\u4e2d\u83b7\u53d6\u51fd\u6570\u5bf9\u8c61\uff0c\u5e76\u4e14\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u83b7\u53d6\u5e76\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for function_name in functions:<\/p>\n<p>    func = getattr(example_module, function_name)<\/p>\n<p>    if callable(func):<\/p>\n<p>        print(f&quot;Calling function {function_name}:&quot;)<\/p>\n<p>        func()  # \u5047\u8bbe\u51fd\u6570\u4e0d\u9700\u8981\u53c2\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u88c5\u9970\u5668\u81ea\u52a8\u6ce8\u518c\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5728\u6a21\u5757\u4e2d\u5b9a\u4e49\u51fd\u6570\u65f6\u81ea\u52a8\u6ce8\u518c\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">registered_functions = []<\/p>\n<p>def register_function(func):<\/p>\n<p>    registered_functions.append(func)<\/p>\n<p>    return func<\/p>\n<p>@register_function<\/p>\n<p>def example_function_1():<\/p>\n<p>    print(&quot;example_function_1 called&quot;)<\/p>\n<p>@register_function<\/p>\n<p>def example_function_2():<\/p>\n<p>    print(&quot;example_function_2 called&quot;)<\/p>\n<p>for func in registered_functions:<\/p>\n<p>    func()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u901a\u8fc7<code>inspect<\/code>\u6a21\u5757\u83b7\u53d6\u51fd\u6570\u8be6\u7ec6\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>Python\u7684<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u6765\u83b7\u53d6\u51fd\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u53c2\u6570\u3001\u9ed8\u8ba4\u503c\u3001\u6ce8\u91ca\u7b49\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u83b7\u53d6\u51fd\u6570\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def get_function_det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls(module):<\/p>\n<p>    function_details = {}<\/p>\n<p>    for function_name in dir(module):<\/p>\n<p>        func = getattr(module, function_name)<\/p>\n<p>        if callable(func):<\/p>\n<p>            sig = inspect.signature(func)<\/p>\n<p>            function_details[function_name] = {<\/p>\n<p>                &quot;name&quot;: function_name,<\/p>\n<p>                &quot;parameters&quot;: sig.parameters,<\/p>\n<p>                &quot;doc&quot;: inspect.getdoc(func)<\/p>\n<p>            }<\/p>\n<p>    return function_details<\/p>\n<p>details = get_function_details(example_module)<\/p>\n<p>for func_name, detail in details.items():<\/p>\n<p>    print(f&quot;Function: {func_name}&quot;)<\/p>\n<p>    print(f&quot;Parameters: {detail[&#39;parameters&#39;]}&quot;)<\/p>\n<p>    print(f&quot;Documentation: {detail[&#39;doc&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u52a8\u751f\u6210\u51fd\u6570\u8c03\u7528\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u81ea\u52a8\u5316\uff0c\u53ef\u4ee5\u751f\u6210\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u7684\u4ee3\u7801\uff0c\u8fd9\u5728\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b\u65f6\u7279\u522b\u6709\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_function_call_code(module):<\/p>\n<p>    code_lines = []<\/p>\n<p>    for function_name in dir(module):<\/p>\n<p>        func = getattr(module, function_name)<\/p>\n<p>        if callable(func):<\/p>\n<p>            sig = inspect.signature(func)<\/p>\n<p>            params = &quot;, &quot;.join([f&quot;{param}={param}&quot; for param in sig.parameters])<\/p>\n<p>            code_lines.append(f&quot;{function_name}({params})&quot;)<\/p>\n<p>    return code_lines<\/p>\n<p>call_code = generate_function_call_code(example_module)<\/p>\n<p>for line in call_code:<\/p>\n<p>    print(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5<strong>\u81ea\u52a8\u5316\u5730\u83b7\u53d6\u5e76\u8c03\u7528Python\u5305\u4e2d\u7684\u6240\u6709\u51fd\u6570<\/strong>\uff0c\u8fd9\u5728\u5927\u89c4\u6a21\u9879\u76ee\u4e2d\u975e\u5e38\u6709\u7528\u3002\u6211\u4eec\u4f7f\u7528\u4e86<code>dir()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u4f7f\u7528<code>getattr()<\/code>\u51fd\u6570\u83b7\u53d6\u8fd9\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u7684\u5f15\u7528\uff0c\u7ed3\u5408<code>inspect<\/code>\u6a21\u5757\u83b7\u53d6\u51fd\u6570\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5e76\u81ea\u52a8\u751f\u6210\u51fd\u6570\u8c03\u7528\u4ee3\u7801\u3002\u8fd9\u6837\u7684\u81ea\u52a8\u5316\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u67e5\u770b\u4e00\u4e2a\u5305\u4e2d\u7684\u6240\u6709\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dir()<\/code>\u51fd\u6570\u7ed3\u5408\u5305\u540d\u6765\u67e5\u770b\u8be5\u5305\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6bd4\u5982\uff0c\u82e5\u8981\u67e5\u770b<code>math<\/code>\u5305\u4e2d\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7<code>import math<\/code>\u540e\u6267\u884c<code>dir(math)<\/code>\u6765\u83b7\u53d6\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u8be5\u5305\u7684\u6240\u6709\u53ef\u7528\u51fd\u6570\u548c\u53d8\u91cf\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u81ea\u52a8\u5217\u51fa\u5305\u4e2d\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5de5\u5177\u548c\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u8f7b\u677e\u5730\u63a2\u7d22Python\u5305\u3002\u4f8b\u5982\uff0c<code>pydoc<\/code>\u662f\u4e00\u4e2a\u5185\u7f6e\u5de5\u5177\uff0c\u53ef\u4ee5\u751f\u6210\u5305\u7684\u6587\u6863\u5e76\u5217\u51fa\u6240\u6709\u7684\u51fd\u6570\u3002\u4f7f\u7528\u547d\u4ee4<code>pydoc &lt;package_name&gt;<\/code>\u53ef\u4ee5\u67e5\u770b\u8be5\u5305\u7684\u8be6\u7ec6\u4fe1\u606f\u548c\u51fd\u6570\u5217\u8868\u3002\u6b64\u5916\uff0cIDE\u5982PyCharm\u548cVS Code\u4e5f\u63d0\u4f9b\u4e86\u81ea\u52a8\u8865\u5168\u548c\u6587\u6863\u67e5\u770b\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528IDE\u6765\u5feb\u901f\u67e5\u770b\u5305\u4e2d\u7684\u51fd\u6570\uff1f<\/strong><br \/>\u8bb8\u591a\u73b0\u4ee3\u7684\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4ee3\u7801\u5bfc\u822a\u529f\u80fd\u3002\u5728PyCharm\u4e2d\uff0c\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u5149\u6807\u653e\u5728\u5305\u540d\u4e0a\uff0c\u6309\u4e0b<code>Ctrl + B<\/code>\uff08\u6216<code>Cmd + B<\/code>\u5728Mac\u4e0a\uff09\u5373\u53ef\u8df3\u8f6c\u5230\u8be5\u5305\u7684\u5b9a\u4e49\uff0c\u5e76\u67e5\u770b\u6240\u6709\u51fd\u6570\u3002\u5728VS Code\u4e2d\uff0c\u4f7f\u7528<code>Ctrl + Shift + O<\/code>\u53ef\u4ee5\u5feb\u901f\u5217\u51fa\u5f53\u524d\u6587\u4ef6\u6216\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u7c7b\uff0c\u5e2e\u52a9\u60a8\u66f4\u5feb\u5730\u627e\u5230\u6240\u9700\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528dir()\u548cgetattr()\u81ea\u52a8\u5e26\u51fa\u5305\u4e2d\u7684\u51fd\u6570 Python\u4e2d\u53ef\u4ee5\u901a\u8fc7dir()\u51fd\u6570\u5217\u51fa\u6a21\u5757\u6216\u5305\u4e2d\u7684\u6240\u6709 [&hellip;]","protected":false},"author":3,"featured_media":1076629,"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\/1076623"}],"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=1076623"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076623\/revisions"}],"predecessor-version":[{"id":1076633,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076623\/revisions\/1076633"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076629"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}