{"id":1160401,"date":"2025-01-13T19:04:24","date_gmt":"2025-01-13T11:04:24","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1160401.html"},"modified":"2025-01-13T19:04:26","modified_gmt":"2025-01-13T11:04:26","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e6%96%b9%e6%b3%95%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1160401.html","title":{"rendered":"python\u5982\u4f55\u6253\u5370\u65b9\u6cd5\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201858\/5b425ca7-0aa1-4be6-a32a-7c4a7e0afbe3.webp\" alt=\"python\u5982\u4f55\u6253\u5370\u65b9\u6cd5\u540d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>__name__<\/code>\u5c5e\u6027\u6765\u6253\u5370\u65b9\u6cd5\u540d\u3001\u7ed3\u5408\u51fd\u6570\u88c5\u9970\u5668\u5b9e\u73b0\u81ea\u52a8\u6253\u5370\u65b9\u6cd5\u540d\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u83b7\u53d6\u65b9\u6cd5\u540d\u3001\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\u6355\u83b7\u8c03\u7528\u5806\u6808\u4fe1\u606f\u7b49\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u662f\u6700\u4e3a\u76f4\u63a5\u4e14\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u6765\u6253\u5370\u65b9\u6cd5\u540d\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u51fd\u6570\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a<code>__name__<\/code>\u7684\u5c5e\u6027\uff0c\u5b83\u5305\u542b\u4e86\u51fd\u6570\u7684\u540d\u79f0\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e2a\u5c5e\u6027\u6765\u83b7\u53d6\u51fd\u6570\u540d\u5e76\u6253\u5370\u51fa\u6765\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function():<\/p>\n<p>    print(&quot;The function name is:&quot;, example_function.__name__)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>example_function<\/code>\u7684\u51fd\u6570\uff0c\u5e76\u5728\u51fd\u6570\u5185\u90e8\u6253\u5370\u5176\u540d\u79f0\u3002\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>The function name is: example_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u83b7\u53d6\u5e76\u6253\u5370\u51fa\u65b9\u6cd5\u540d\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u51e0\u79cd\u83b7\u53d6\u65b9\u6cd5\u540d\u7684\u6280\u672f\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u51fd\u6570\u5bf9\u8c61\u90fd\u6709\u4e00\u4e2a\u540d\u4e3a<code>__name__<\/code>\u7684\u5c5e\u6027\uff0c\u5b83\u5305\u542b\u4e86\u51fd\u6570\u7684\u540d\u79f0\u3002\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8fd9\u4e2a\u5c5e\u6027\u6765\u83b7\u53d6\u51fd\u6570\u540d\u5e76\u6253\u5370\u51fa\u6765\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u66f4\u8be6\u7ec6\u7684\u4f8b\u5b50\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h2>1. \u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>\u57fa\u672c\u7528\u6cd5\u662f\u76f4\u63a5\u5728\u51fd\u6570\u5185\u90e8\u6253\u5370<code>__name__<\/code>\u5c5e\u6027\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def example_function():<\/p>\n<p>    print(&quot;The function name is:&quot;, example_function.__name__)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>The function name is: example_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u7b80\u5355\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u548c\u6253\u5370\u51fd\u6570\u540d\u3002<\/p>\n<\/p>\n<p><h2>2. \u5728\u88c5\u9970\u5668\u4e2d\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662fPython\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u5728\u51fd\u6570\u6267\u884c\u4e4b\u524d\u6216\u4e4b\u540e\u6dfb\u52a0\u989d\u5916\u7684\u884c\u4e3a\u3002\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\u6765\u81ea\u52a8\u6253\u5370\u88ab\u88c5\u9970\u51fd\u6570\u7684\u540d\u79f0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_function_name(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(&quot;The function name is:&quot;, func.__name__)<\/p>\n<p>        return func(*args, kwargs)<\/p>\n<p>    return wrapper<\/p>\n<p>@print_function_name<\/p>\n<p>def example_function():<\/p>\n<p>    print(&quot;This is the example function&quot;)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>The function name is: example_function<\/p>\n<p>This is the example function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u52a8\u6253\u5370\u4efb\u4f55\u88ab\u88c5\u9970\u51fd\u6570\u7684\u540d\u79f0\uff0c\u800c\u65e0\u9700\u5728\u6bcf\u4e2a\u51fd\u6570\u4e2d\u624b\u52a8\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\u3002<\/p>\n<\/p>\n<p><h2>3. \u5728\u7c7b\u65b9\u6cd5\u4e2d\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u540c\u6837\u53ef\u4ee5\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u6765\u83b7\u53d6\u65b9\u6cd5\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class ExampleClass:<\/p>\n<p>    def example_method(self):<\/p>\n<p>        print(&quot;The method name is:&quot;, self.example_method.__name__)<\/p>\n<p>example_instance = ExampleClass()<\/p>\n<p>example_instance.example_method()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>The method name is: example_method<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u8868\u660e\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u65b9\u5f0f\u6765\u83b7\u53d6\u548c\u6253\u5370\u65b9\u6cd5\u540d\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e8c\u3001\u7ed3\u5408\u51fd\u6570\u88c5\u9970\u5668\u5b9e\u73b0\u81ea\u52a8\u6253\u5370\u65b9\u6cd5\u540d<\/strong><\/h2>\n<p><p>\u88c5\u9970\u5668\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u6253\u5370\u51fd\u6570\u540d\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\u3002\u901a\u8fc7\u7ed3\u5408\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u51fd\u6570\u6267\u884c\u7684\u4e0d\u540c\u9636\u6bb5\u6253\u5370\u4fe1\u606f\uff0c\u751a\u81f3\u53ef\u4ee5\u8bb0\u5f55\u65e5\u5fd7\u3001\u8ba1\u7b97\u6267\u884c\u65f6\u95f4\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u4f7f\u7528\u88c5\u9970\u5668\u7684\u9ad8\u7ea7\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h2>1. \u6253\u5370\u51fd\u6570\u540d\u548c\u53c2\u6570<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\u6765\u6253\u5370\u51fd\u6570\u540d\u4ee5\u53ca\u4f20\u9012\u7ed9\u51fd\u6570\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def print_function_det<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Function name: {func.__name__}&quot;)<\/p>\n<p>        print(f&quot;Arguments: {args}, Keyword arguments: {kwargs}&quot;)<\/p>\n<p>        return func(*args, kwargs)<\/p>\n<p>    return wrapper<\/p>\n<p>@print_function_details<\/p>\n<p>def example_function(a, b, c=3):<\/p>\n<p>    print(&quot;Function body execution&quot;)<\/p>\n<p>example_function(1, 2, c=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Function name: example_function<\/p>\n<p>Arguments: (1, 2), Keyword arguments: {&#39;c&#39;: 4}<\/p>\n<p>Function body execution<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8c03\u8bd5\u548c\u65e5\u5fd7\u8bb0\u5f55\u65f6\u66f4\u597d\u5730\u4e86\u89e3\u51fd\u6570\u7684\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>2. \u8ba1\u7b97\u51fd\u6570\u6267\u884c\u65f6\u95f4<\/h2>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u88c5\u9970\u5668\u7528\u9014\u662f\u8ba1\u7b97\u51fd\u6570\u7684\u6267\u884c\u65f6\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def measure_time(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;Function {func.__name__} took {end_time - start_time} seconds&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@measure_time<\/p>\n<p>def example_function():<\/p>\n<p>    time.sleep(2)<\/p>\n<p>    print(&quot;Function body execution&quot;)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Function body execution<\/p>\n<p>Function example_function took 2.0 seconds<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4f18\u5316\u4ee3\u7801\u6027\u80fd\uff0c\u627e\u51fa\u6267\u884c\u65f6\u95f4\u8f83\u957f\u7684\u51fd\u6570\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e09\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u83b7\u53d6\u65b9\u6cd5\u540d<\/strong><\/h2>\n<p><p>Python\u7684<code>inspect<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u51fd\u6570\u6765\u68c0\u67e5\u5b9e\u65f6\u5bf9\u8c61\uff0c\u5305\u62ec\u83b7\u53d6\u51fd\u6570\u7684\u540d\u79f0\u3001\u53c2\u6570\u3001\u6e90\u4ee3\u7801\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>1. \u83b7\u53d6\u51fd\u6570\u540d\u79f0<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u51fd\u6570\u7684\u540d\u79f0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def example_function():<\/p>\n<p>    frame = inspect.currentframe()<\/p>\n<p>    print(&quot;The function name is:&quot;, frame.f_code.co_name)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>The function name is: example_function<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u6bd4\u76f4\u63a5\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027\u66f4\u590d\u6742\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h2>2. \u83b7\u53d6\u8c03\u7528\u5806\u6808<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u6765\u83b7\u53d6\u8c03\u7528\u5806\u6808\uff0c\u4ece\u800c\u4e86\u89e3\u51fd\u6570\u662f\u5982\u4f55\u88ab\u8c03\u7528\u7684\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import inspect<\/p>\n<p>def print_call_stack():<\/p>\n<p>    stack = inspect.stack()<\/p>\n<p>    for frame_info in stack:<\/p>\n<p>        print(f&quot;Function {frame_info.function} called from {frame_info.filename}:{frame_info.lineno}&quot;)<\/p>\n<p>def example_function():<\/p>\n<p>    print_call_stack()<\/p>\n<p>def another_function():<\/p>\n<p>    example_function()<\/p>\n<p>another_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\u7c7b\u4f3c\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code>Function print_call_stack called from &lt;ipython-input-10-7a1d4b0c2b3d&gt;:6<\/p>\n<p>Function example_function called from &lt;ipython-input-10-7a1d4b0c2b3d&gt;:10<\/p>\n<p>Function another_function called from &lt;ipython-input-10-7a1d4b0c2b3d&gt;:13<\/p>\n<p>Function &lt;module&gt; called from &lt;ipython-input-10-7a1d4b0c2b3d&gt;:15<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u51fd\u6570\u8c03\u7528\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u65b9\u4fbf\u8fdb\u884c\u8c03\u8bd5\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\u6355\u83b7\u8c03\u7528\u5806\u6808\u4fe1\u606f<\/strong><\/h2>\n<p><p><code>traceback<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\u6765\u5904\u7406\u548c\u6253\u5370\u5f02\u5e38\u7684\u8c03\u7528\u5806\u6808\u4fe1\u606f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\u6765\u6355\u83b7\u548c\u6253\u5370\u51fd\u6570\u7684\u8c03\u7528\u5806\u6808\u3002<\/p>\n<\/p>\n<p><h2>1. \u6355\u83b7\u8c03\u7528\u5806\u6808<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\u6765\u6355\u83b7\u548c\u6253\u5370\u51fd\u6570\u7684\u8c03\u7528\u5806\u6808\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import traceback<\/p>\n<p>def print_call_stack():<\/p>\n<p>    stack = traceback.format_stack()<\/p>\n<p>    print(&quot;Call stack:&quot;)<\/p>\n<p>    for frame in stack:<\/p>\n<p>        print(frame.strip())<\/p>\n<p>def example_function():<\/p>\n<p>    print_call_stack()<\/p>\n<p>def another_function():<\/p>\n<p>    example_function()<\/p>\n<p>another_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\u7c7b\u4f3c\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code>Call stack:<\/p>\n<p>File &quot;&lt;ipython-input-12-6e4a1b7f5f3a&gt;&quot;, line 13, in &lt;module&gt;<\/p>\n<p>    another_function()<\/p>\n<p>File &quot;&lt;ipython-input-12-6e4a1b7f5f3a&gt;&quot;, line 11, in another_function<\/p>\n<p>    example_function()<\/p>\n<p>File &quot;&lt;ipython-input-12-6e4a1b7f5f3a&gt;&quot;, line 8, in example_function<\/p>\n<p>    print_call_stack()<\/p>\n<p>File &quot;&lt;ipython-input-12-6e4a1b7f5f3a&gt;&quot;, line 5, in print_call_stack<\/p>\n<p>    stack = traceback.format_stack()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u6355\u83b7\u5f02\u5e38\u65f6\u4e86\u89e3\u5b8c\u6574\u7684\u8c03\u7528\u8def\u5f84\uff0c\u6709\u52a9\u4e8e\u5b9a\u4f4d\u95ee\u9898\u548c\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><h2>2. \u6355\u83b7\u5f02\u5e38\u4fe1\u606f<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u6355\u83b7\u8c03\u7528\u5806\u6808\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\u6765\u6355\u83b7\u548c\u6253\u5370\u5f02\u5e38\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import traceback<\/p>\n<p>def example_function():<\/p>\n<p>    try:<\/p>\n<p>        1 \/ 0<\/p>\n<p>    except ZeroDivisionError:<\/p>\n<p>        traceback.print_exc()<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\u7c7b\u4f3c\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code>Traceback (most recent call last):<\/p>\n<p>  File &quot;&lt;ipython-input-13-4d1c5a6a7b4c&gt;&quot;, line 5, in example_function<\/p>\n<p>    1 \/ 0<\/p>\n<p>ZeroDivisionError: division by zero<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u5904\u7406\u5f02\u5e38\u65f6\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u65b9\u4fbf\u8c03\u8bd5\u548c\u4fee\u590d\u95ee\u9898\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e94\u3001\u7ed3\u5408\u65e5\u5fd7\u7cfb\u7edf\u6253\u5370\u65b9\u6cd5\u540d<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u4f7f\u7528\u65e5\u5fd7\u7cfb\u7edf\u6765\u8bb0\u5f55\u51fd\u6570\u7684\u6267\u884c\u4fe1\u606f\u662f\u975e\u5e38\u5e38\u89c1\u7684\u505a\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408Python\u7684<code>logging<\/code>\u6a21\u5757\u6765\u6253\u5370\u65b9\u6cd5\u540d\u5e76\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><h2>1. \u57fa\u672c\u65e5\u5fd7\u8bb0\u5f55<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>logging<\/code>\u6a21\u5757\u6765\u8bb0\u5f55\u57fa\u672c\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u5305\u62ec\u51fd\u6570\u540d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>def example_function():<\/p>\n<p>    logging.info(f&quot;The function name is: {example_function.__name__}&quot;)<\/p>\n<p>    logging.info(&quot;This is an example log message&quot;)<\/p>\n<p>example_function()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>INFO:root:The function name is: example_function<\/p>\n<p>INFO:root:This is an example log message<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u8c03\u8bd5\u548c\u8fd0\u884c\u65f6\u8bb0\u5f55\u51fd\u6570\u7684\u6267\u884c\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>2. \u4f7f\u7528\u88c5\u9970\u5668\u7ed3\u5408\u65e5\u5fd7\u7cfb\u7edf<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\u6765\u81ea\u52a8\u8bb0\u5f55\u88ab\u88c5\u9970\u51fd\u6570\u7684\u6267\u884c\u4fe1\u606f\uff0c\u5305\u62ec\u51fd\u6570\u540d\u548c\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>def log_function_details(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        logging.info(f&quot;Function name: {func.__name__}&quot;)<\/p>\n<p>        logging.info(f&quot;Arguments: {args}, Keyword arguments: {kwargs}&quot;)<\/p>\n<p>        return func(*args, kwargs)<\/p>\n<p>    return wrapper<\/p>\n<p>@log_function_details<\/p>\n<p>def example_function(a, b, c=3):<\/p>\n<p>    logging.info(&quot;Function body execution&quot;)<\/p>\n<p>example_function(1, 2, c=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>INFO:root:Function name: example_function<\/p>\n<p>INFO:root:Arguments: (1, 2), Keyword arguments: {&#39;c&#39;: 4}<\/p>\n<p>INFO:root:Function body execution<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed3\u5408\u65e5\u5fd7\u7cfb\u7edf\u548c\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u52a8\u8bb0\u5f55\u51fd\u6570\u7684\u6267\u884c\u4fe1\u606f\uff0c\u65b9\u4fbf\u8fdb\u884c\u8c03\u8bd5\u548c\u76d1\u63a7\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u516d\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u548c\u6253\u5370\u65b9\u6cd5\u540d\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>__name__<\/code>\u5c5e\u6027\u3001\u7ed3\u5408\u51fd\u6570\u88c5\u9970\u5668\u5b9e\u73b0\u81ea\u52a8\u6253\u5370\u65b9\u6cd5\u540d\u3001\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757\u83b7\u53d6\u65b9\u6cd5\u540d\u3001\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\u6355\u83b7\u8c03\u7528\u5806\u6808\u4fe1\u606f\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>__name__<\/code>\u5c5e\u6027<\/strong>\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u5e38\u89c1\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u7ed3\u5408\u51fd\u6570\u88c5\u9970\u5668<\/strong>\u53ef\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u81ea\u52a8\u6253\u5370\u65b9\u6cd5\u540d\u548c\u53c2\u6570\u3001\u8ba1\u7b97\u6267\u884c\u65f6\u95f4\u7b49\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u51fd\u6570\u6267\u884c\u7684\u4e0d\u540c\u9636\u6bb5\u6dfb\u52a0\u989d\u5916\u884c\u4e3a\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>inspect<\/code>\u6a21\u5757<\/strong>\u53ef\u4ee5\u83b7\u53d6\u66f4\u8be6\u7ec6\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\u8c03\u7528\u5806\u6808\u3001\u51fd\u6570\u53c2\u6570\u7b49\uff0c\u9002\u7528\u4e8e\u9700\u8981\u7075\u6d3b\u83b7\u53d6\u51fd\u6570\u4fe1\u606f\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757<\/strong>\u53ef\u4ee5\u6355\u83b7\u548c\u6253\u5370\u5f02\u5e38\u7684\u8c03\u7528\u5806\u6808\u4fe1\u606f\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u65f6\u9700\u8981\u4e86\u89e3\u5b8c\u6574\u8c03\u7528\u8def\u5f84\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p><strong>\u7ed3\u5408\u65e5\u5fd7\u7cfb\u7edf<\/strong>\u53ef\u4ee5\u8bb0\u5f55\u51fd\u6570\u7684\u6267\u884c\u4fe1\u606f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5728\u8c03\u8bd5\u548c\u8fd0\u884c\u65f6\u8bb0\u5f55\u65e5\u5fd7\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u83b7\u53d6\u548c\u6253\u5370\u65b9\u6cd5\u540d\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\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\u83b7\u53d6\u5f53\u524d\u6267\u884c\u7684\u51fd\u6570\u540d\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u6a21\u5757<code>inspect<\/code>\u6765\u83b7\u53d6\u5f53\u524d\u6267\u884c\u7684\u51fd\u6570\u540d\u3002\u901a\u8fc7<code>inspect.currentframe()<\/code>\u548c<code>inspect.getframeinfo()<\/code>\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import inspect\n\ndef my_function():\n    frame = inspect.currentframe()\n    function_name = inspect.getframeinfo(frame).function\n    print(f&quot;\u5f53\u524d\u51fd\u6570\u540d\u662f: {function_name}&quot;)\n\nmy_function()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u6253\u5370\u51fa\u5f53\u524d\u51fd\u6570\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6253\u5370\u7c7b\u65b9\u6cd5\u7684\u540d\u79f0\uff1f<\/strong><br \/>\u82e5\u8981\u6253\u5370\u7c7b\u4e2d\u7684\u65b9\u6cd5\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>self.__class__.__name__<\/code>\u7ed3\u5408<code>__name__<\/code>\u5c5e\u6027\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def my_method(self):\n        method_name = self.my_method.__name__\n        print(f&quot;\u5f53\u524d\u65b9\u6cd5\u540d\u662f: {method_name}&quot;)\n\nobj = MyClass()\nobj.my_method()\n<\/code><\/pre>\n<p>\u8fd0\u884c\u8fd9\u6bb5\u4ee3\u7801\u65f6\uff0c\u5c06\u8f93\u51fa\u8be5\u65b9\u6cd5\u7684\u540d\u79f0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5370\u6240\u6709\u53ef\u7528\u65b9\u6cd5\u7684\u540d\u79f0\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>dir()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u5c5e\u6027\u548c\u65b9\u6cd5\u540d\u3002\u7ed3\u5408<code>callable()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7b5b\u9009\u51fa\u53ef\u8c03\u7528\u7684\u65b9\u6cd5\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">class MyClass:\n    def method_one(self):\n        pass\n    \n    def method_two(self):\n        pass\n\nmethods = [method for method in dir(MyClass) if callable(getattr(MyClass, method)) and not method.startswith(&quot;__&quot;)]\nprint(&quot;\u53ef\u7528\u65b9\u6cd5\u6709:&quot;, methods)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5370\u51fa<code>MyClass<\/code>\u7c7b\u4e2d\u6240\u6709\u53ef\u8c03\u7528\u7684\u65b9\u6cd5\u540d\uff0c\u65b9\u4fbf\u67e5\u770b\u548c\u8c03\u8bd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684__name__\u5c5e\u6027\u6765\u6253\u5370\u65b9\u6cd5\u540d\u3001\u7ed3\u5408\u51fd\u6570\u88c5\u9970\u5668\u5b9e\u73b0\u81ea\u52a8\u6253\u5370\u65b9\u6cd5\u540d\u3001\u4f7f\u7528ins [&hellip;]","protected":false},"author":3,"featured_media":1160405,"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\/1160401"}],"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=1160401"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160401\/revisions"}],"predecessor-version":[{"id":1160407,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1160401\/revisions\/1160407"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1160405"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1160401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1160401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1160401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}