{"id":1073787,"date":"2025-01-08T11:28:47","date_gmt":"2025-01-08T03:28:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073787.html"},"modified":"2025-01-08T11:28:49","modified_gmt":"2025-01-08T03:28:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%9c%8b%e6%9c%89%e5%a4%9a%e5%b0%91%e4%b8%aa%e5%87%bd%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1073787.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u770b\u6709\u591a\u5c11\u4e2a\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103621\/aa42fca0-e9ed-4335-99ed-c547217121e7.webp\" alt=\"python\u4e2d\u5982\u4f55\u770b\u6709\u591a\u5c11\u4e2a\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u6709\u591a\u5c11\u4e2a\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\uff0c\u751a\u81f3\u7f16\u5199\u81ea\u5b9a\u4e49\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528inspect\u6a21\u5757\u3001\u4f7f\u7528dir()\u51fd\u6570\u3001\u4f7f\u7528ast\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528inspect\u6a21\u5757\u662f\u6700\u8be6\u7ec6\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528inspect\u6a21\u5757<\/strong><\/p>\n<p>Python\u7684inspect\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u83b7\u53d6\u6709\u5173\u5bf9\u8c61\u7684\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u83b7\u53d6\u4e00\u4e2a\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import module_name<\/p>\n<p>def get_functions(module):<\/p>\n<p>    return [name for name, obj in inspect.getmembers(module) if inspect.isfunction(obj)]<\/p>\n<p>functions = get_functions(module_name)<\/p>\n<p>print(f&quot;Number of functions: {len(functions)}&quot;)<\/p>\n<p>print(f&quot;Functions: {functions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165inspect\u6a21\u5757\u548c\u76ee\u6807\u6a21\u5757\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aget_functions\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u83b7\u53d6\u6a21\u5757\u4e2d\u6240\u6709\u51fd\u6570\u7684\u540d\u79f0\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u51fa\u51fd\u6570\u7684\u6570\u91cf\u548c\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528dir()\u51fd\u6570<\/strong><\/p>\n<p>dir()\u51fd\u6570\u8fd4\u56de\u6307\u5b9a\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u7136\u540e\u7b5b\u9009\u51fa\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import module_name<\/p>\n<p>def get_functions(module):<\/p>\n<p>    return [attr for attr in dir(module) if callable(getattr(module, attr))]<\/p>\n<p>functions = get_functions(module_name)<\/p>\n<p>print(f&quot;Number of functions: {len(functions)}&quot;)<\/p>\n<p>print(f&quot;Functions: {functions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u76ee\u6807\u6a21\u5757\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aget_functions\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u83b7\u53d6\u6a21\u5757\u4e2d\u6240\u6709\u53ef\u8c03\u7528\u7684\u5c5e\u6027\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u51fa\u51fd\u6570\u7684\u6570\u91cf\u548c\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528ast\u6a21\u5757<\/strong><\/p>\n<p>ast\u6a21\u5757\u7528\u4e8e\u5904\u7406\u62bd\u8c61\u8bed\u6cd5\u6811\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u89e3\u6790Python\u6e90\u4ee3\u7801\u5e76\u63d0\u53d6\u51fd\u6570\u5b9a\u4e49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>def get_functions_from_code(code):<\/p>\n<p>    tree = ast.parse(code)<\/p>\n<p>    return [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]<\/p>\n<p>with open(&quot;module_name.py&quot;, &quot;r&quot;) as file:<\/p>\n<p>    code = file.read()<\/p>\n<p>functions = get_functions_from_code(code)<\/p>\n<p>print(f&quot;Number of functions: {len(functions)}&quot;)<\/p>\n<p>print(f&quot;Functions: {functions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165ast\u6a21\u5757\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3aget_functions_from_code\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u89e3\u6790Python\u6e90\u4ee3\u7801\u5e76\u63d0\u53d6\u51fd\u6570\u5b9a\u4e49\u3002\u6700\u540e\uff0c\u6211\u4eec\u8bfb\u53d6\u76ee\u6807\u6a21\u5757\u7684\u6e90\u4ee3\u7801\uff0c\u5e76\u6253\u5370\u51fa\u51fd\u6570\u7684\u6570\u91cf\u548c\u540d\u79f0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528inspect\u6a21\u5757<\/h3>\n<\/p>\n<p><p>inspect\u6a21\u5757\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u83b7\u53d6\u6709\u5173\u5bf9\u8c61\u7684\u4fe1\u606f\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u3001\u7c7b\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528inspect.getmembers()\u51fd\u6570\u6765\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u7136\u540e\u4f7f\u7528inspect.isfunction()\u51fd\u6570\u7b5b\u9009\u51fa\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import module_name<\/p>\n<p>def get_functions(module):<\/p>\n<p>    return [name for name, obj in inspect.getmembers(module) if inspect.isfunction(obj)]<\/p>\n<p>functions = get_functions(module_name)<\/p>\n<p>print(f&quot;Number of functions: {len(functions)}&quot;)<\/p>\n<p>print(f&quot;Functions: {functions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cinspect.getmembers()\u51fd\u6570\u8fd4\u56de\u6a21\u5757\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u5305\u62ec\u51fd\u6570\u3001\u7c7b\u548c\u53d8\u91cf\u3002\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548cinspect.isfunction()\u51fd\u6570\u7b5b\u9009\u51fa\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>import module_name<\/p>\n<p>def get_methods(cls):<\/p>\n<p>    return [name for name, obj in inspect.getmembers(cls) if inspect.isfunction(obj)]<\/p>\n<p>methods = get_methods(module_name.ClassName)<\/p>\n<p>print(f&quot;Number of methods: {len(methods)}&quot;)<\/p>\n<p>print(f&quot;Methods: {methods}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528inspect.getmembers()\u51fd\u6570\u83b7\u53d6\u7c7b\u4e2d\u7684\u6240\u6709\u6210\u5458\uff0c\u7136\u540e\u4f7f\u7528inspect.isfunction()\u51fd\u6570\u7b5b\u9009\u51fa\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528inspect\u6a21\u5757\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def get_attributes_and_methods(obj):<\/p>\n<p>    return [name for name, obj in inspect.getmembers(obj) if not name.startswith(&quot;__&quot;)]<\/p>\n<p>obj = module_name.ClassName()<\/p>\n<p>attributes_and_methods = get_attributes_and_methods(obj)<\/p>\n<p>print(f&quot;Attributes and methods: {attributes_and_methods}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528inspect.getmembers()\u51fd\u6570\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458\uff0c\u5e76\u6392\u9664\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u7279\u6b8a\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528dir()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>dir()\u51fd\u6570\u8fd4\u56de\u6307\u5b9a\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5217\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u7136\u540e\u7b5b\u9009\u51fa\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528dir()\u51fd\u6570\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u7136\u540e\u4f7f\u7528callable()\u51fd\u6570\u7b5b\u9009\u51fa\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import module_name<\/p>\n<p>def get_functions(module):<\/p>\n<p>    return [attr for attr in dir(module) if callable(getattr(module, attr))]<\/p>\n<p>functions = get_functions(module_name)<\/p>\n<p>print(f&quot;Number of functions: {len(functions)}&quot;)<\/p>\n<p>print(f&quot;Functions: {functions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cdir()\u51fd\u6570\u8fd4\u56de\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548ccallable()\u51fd\u6570\u7b5b\u9009\u51fa\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u83b7\u53d6\u7c7b\u4e2d\u7684\u6240\u6709\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import module_name<\/p>\n<p>def get_methods(cls):<\/p>\n<p>    return [attr for attr in dir(cls) if callable(getattr(cls, attr))]<\/p>\n<p>methods = get_methods(module_name.ClassName)<\/p>\n<p>print(f&quot;Number of methods: {len(methods)}&quot;)<\/p>\n<p>print(f&quot;Methods: {methods}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cdir()\u51fd\u6570\u8fd4\u56de\u7c7b\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548ccallable()\u51fd\u6570\u7b5b\u9009\u51fa\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528dir()\u51fd\u6570\u6765\u83b7\u53d6\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import module_name<\/p>\n<p>def get_attributes_and_methods(obj):<\/p>\n<p>    return [attr for attr in dir(obj) if not attr.startswith(&quot;__&quot;)]<\/p>\n<p>obj = module_name.ClassName()<\/p>\n<p>attributes_and_methods = get_attributes_and_methods(obj)<\/p>\n<p>print(f&quot;Attributes and methods: {attributes_and_methods}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cdir()\u51fd\u6570\u8fd4\u56de\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u3002\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u5e76\u6392\u9664\u4ee5\u53cc\u4e0b\u5212\u7ebf\u5f00\u5934\u7684\u7279\u6b8a\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528ast\u6a21\u5757<\/h3>\n<\/p>\n<p><p>ast\u6a21\u5757\u7528\u4e8e\u5904\u7406\u62bd\u8c61\u8bed\u6cd5\u6811\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u89e3\u6790Python\u6e90\u4ee3\u7801\u5e76\u63d0\u53d6\u51fd\u6570\u5b9a\u4e49\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u89e3\u6790Python\u6e90\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ast.parse()\u51fd\u6570\u6765\u89e3\u6790Python\u6e90\u4ee3\u7801\uff0c\u5e76\u751f\u6210\u62bd\u8c61\u8bed\u6cd5\u6811\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>with open(&quot;module_name.py&quot;, &quot;r&quot;) as file:<\/p>\n<p>    code = file.read()<\/p>\n<p>tree = ast.parse(code)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528ast.parse()\u51fd\u6570\u89e3\u6790\u76ee\u6807\u6a21\u5757\u7684\u6e90\u4ee3\u7801\uff0c\u5e76\u751f\u6210\u62bd\u8c61\u8bed\u6cd5\u6811\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u63d0\u53d6\u51fd\u6570\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528ast.walk()\u51fd\u6570\u904d\u5386\u62bd\u8c61\u8bed\u6cd5\u6811\uff0c\u5e76\u63d0\u53d6\u51fd\u6570\u5b9a\u4e49\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>def get_functions_from_code(code):<\/p>\n<p>    tree = ast.parse(code)<\/p>\n<p>    return [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]<\/p>\n<p>with open(&quot;module_name.py&quot;, &quot;r&quot;) as file:<\/p>\n<p>    code = file.read()<\/p>\n<p>functions = get_functions_from_code(code)<\/p>\n<p>print(f&quot;Number of functions: {len(functions)}&quot;)<\/p>\n<p>print(f&quot;Functions: {functions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cast.walk()\u51fd\u6570\u904d\u5386\u62bd\u8c61\u8bed\u6cd5\u6811\uff0c\u5e76\u8fd4\u56de\u6240\u6709\u8282\u70b9\u3002\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548cisinstance()\u51fd\u6570\u7b5b\u9009\u51fa\u51fd\u6570\u5b9a\u4e49\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u63d0\u53d6\u7c7b\u4e2d\u7684\u65b9\u6cd5\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u65b9\u6cd5\u6765\u63d0\u53d6\u7c7b\u4e2d\u7684\u65b9\u6cd5\u5b9a\u4e49\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ast<\/p>\n<p>def get_methods_from_code(code):<\/p>\n<p>    tree = ast.parse(code)<\/p>\n<p>    methods = []<\/p>\n<p>    for node in ast.walk(tree):<\/p>\n<p>        if isinstance(node, ast.ClassDef):<\/p>\n<p>            for sub_node in node.body:<\/p>\n<p>                if isinstance(sub_node, ast.FunctionDef):<\/p>\n<p>                    methods.append(sub_node.name)<\/p>\n<p>    return methods<\/p>\n<p>with open(&quot;module_name.py&quot;, &quot;r&quot;) as file:<\/p>\n<p>    code = file.read()<\/p>\n<p>methods = get_methods_from_code(code)<\/p>\n<p>print(f&quot;Number of methods: {len(methods)}&quot;)<\/p>\n<p>print(f&quot;Methods: {methods}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u62bd\u8c61\u8bed\u6cd5\u6811\uff0c\u5e76\u63d0\u53d6\u7c7b\u5b9a\u4e49\u8282\u70b9\u3002\u7136\u540e\uff0c\u6211\u4eec\u904d\u5386\u7c7b\u5b9a\u4e49\u8282\u70b9\u7684\u5b50\u8282\u70b9\uff0c\u5e76\u7b5b\u9009\u51fa\u65b9\u6cd5\u5b9a\u4e49\u8282\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u6709\u591a\u5c11\u4e2a\u51fd\u6570\u3002\u4f7f\u7528inspect\u6a21\u5757\u662f\u6700\u8be6\u7ec6\u548c\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u51fd\u6570\u3001\u7c7b\u548c\u65b9\u6cd5\u3002\u4f7f\u7528dir()\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u6a21\u5757\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\uff0c\u5e76\u7b5b\u9009\u51fa\u51fd\u6570\u3002\u4f7f\u7528ast\u6a21\u5757\u53ef\u4ee5\u89e3\u6790Python\u6e90\u4ee3\u7801\uff0c\u5e76\u63d0\u53d6\u51fd\u6570\u548c\u65b9\u6cd5\u5b9a\u4e49\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u6765\u67e5\u770b\u51fd\u6570\u6570\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u67e5\u770bPython\u4ee3\u7801\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dir()<\/code>\u548c<code>inspect<\/code>\u6a21\u5757\u6765\u67e5\u770b\u5f53\u524d\u6a21\u5757\u6216\u5bf9\u8c61\u4e2d\u5b9a\u4e49\u7684\u6240\u6709\u51fd\u6570\u3002\u9996\u5148\uff0c\u5bfc\u5165<code>inspect<\/code>\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>inspect.getmembers()<\/code>\u51fd\u6570\uff0c\u7ed3\u5408<code>inspect.isfunction()<\/code>\u6765\u7b5b\u9009\u51fa\u6240\u6709\u7684\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import inspect\n\ndef sample_function():\n    pass\n\nclass SampleClass:\n    def method_one(self):\n        pass\n    \n    def method_two(self):\n        pass\n\n# \u67e5\u770b\u5f53\u524d\u6a21\u5757\u7684\u6240\u6709\u51fd\u6570\nfunctions = inspect.getmembers(SampleClass, inspect.isfunction)\nprint(functions)\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7IDLE\u6216\u5176\u4ed6IDE\u67e5\u770b\u51fd\u6570\u6570\u91cf\uff1f<\/strong><br \/>\u8bb8\u591a\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\uff0c\u5982PyCharm\u3001VSCode\u7b49\uff0c\u63d0\u4f9b\u4e86\u4ee3\u7801\u5bfc\u822a\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u67e5\u770b\u5f53\u524d\u6587\u4ef6\u6216\u6574\u4e2a\u9879\u76ee\u4e2d\u5b9a\u4e49\u7684\u51fd\u6570\u3002\u901a\u5e38\uff0cIDE\u7684\u4fa7\u8fb9\u680f\u4f1a\u6709\u4e00\u4e2a\u201c\u7ed3\u6784\u201d\u6216\u201c\u5927\u7eb2\u201d\u89c6\u56fe\uff0c\u5217\u51fa\u6240\u6709\u51fd\u6570\u53ca\u5176\u5b9a\u4e49\u4f4d\u7f6e\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u4e86\u89e3\u4ee3\u7801\u7ed3\u6784\u548c\u51fd\u6570\u6570\u91cf\u3002<\/p>\n<p><strong>\u5bf9\u4e8e\u5927\u578b\u9879\u76ee\uff0c\u5982\u4f55\u9ad8\u6548\u7edf\u8ba1\u6240\u6709\u51fd\u6570\uff1f<\/strong><br \/>\u5728\u5927\u578b\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u811a\u672c\u81ea\u52a8\u5316\u7edf\u8ba1\u51fd\u6570\u6570\u91cf\u3002\u901a\u8fc7\u904d\u5386\u9879\u76ee\u4e2d\u7684\u6240\u6709Python\u6587\u4ef6\uff0c\u7ed3\u5408<code>ast<\/code>\u6a21\u5757\uff08\u62bd\u8c61\u8bed\u6cd5\u6811\uff09\u6765\u5206\u6790\u4ee3\u7801\u7ed3\u6784\uff0c\u53ef\u4ee5\u6709\u6548\u83b7\u53d6\u6bcf\u4e2a\u6587\u4ef6\u4e2d\u7684\u51fd\u6570\u5b9a\u4e49\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import ast\nimport os\n\ndef count_functions_in_file(filepath):\n    with open(filepath, &#39;r&#39;) as file:\n        node = ast.parse(file.read())\n        return len([n for n in ast.walk(node) if isinstance(n, ast.FunctionDef)])\n\ndef count_functions_in_directory(directory):\n    total_functions = 0\n    for filename in os.listdir(directory):\n        if filename.endswith(&#39;.py&#39;):\n            total_functions += count_functions_in_file(os.path.join(directory, filename))\n    return total_functions\n\n# \u8c03\u7528\u51fd\u6570\u5e76\u4f20\u5165\u9879\u76ee\u76ee\u5f55\nprint(count_functions_in_directory(&#39;\/path\/to\/your\/project&#39;))\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u6574\u4e2a\u9879\u76ee\u4e2d\u6240\u6709\u51fd\u6570\u7684\u6570\u91cf\uff0c\u8282\u7701\u4e86\u624b\u52a8\u68c0\u67e5\u7684\u65f6\u95f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u67e5\u770b\u6709\u591a\u5c11\u4e2a\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6a21\u5757\u3001\u7b2c\u4e09\u65b9\u5e93\uff0c\u751a\u81f3\u7f16\u5199\u81ea\u5b9a\u4e49\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1073798,"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\/1073787"}],"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=1073787"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073787\/revisions"}],"predecessor-version":[{"id":1073799,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1073787\/revisions\/1073799"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1073798"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1073787"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1073787"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1073787"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}