{"id":1151376,"date":"2025-01-13T17:12:52","date_gmt":"2025-01-13T09:12:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1151376.html"},"modified":"2025-01-13T17:12:55","modified_gmt":"2025-01-13T09:12:55","slug":"python-%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1151376.html","title":{"rendered":"python \u5982\u4f55\u6253\u5370\u51fd\u6570\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25181632\/2c67dfe9-bdd8-4576-b864-6a9b92135a58.webp\" alt=\"python \u5982\u4f55\u6253\u5370\u51fd\u6570\u8c03\u7528\" \/><\/p>\n<p><p> <strong>\u8981\u6253\u5370\u51fd\u6570\u8c03\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528print\u8bed\u53e5\u3001logging\u6a21\u5757\u3001\u88c5\u9970\u5668\u7b49\u65b9\u6cd5\u3002<\/strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7print\u8bed\u53e5\u76f4\u63a5\u5728\u51fd\u6570\u5185\u6dfb\u52a0\u6253\u5370\u4fe1\u606f\uff0c\u6216\u901a\u8fc7\u88c5\u9970\u5668\u7edf\u4e00\u7ba1\u7406\u51fd\u6570\u8c03\u7528\u7684\u6253\u5370\u3002<strong>\u88c5\u9970\u5668\u65b9\u6cd5<\/strong>\u66f4\u4e3a\u63a8\u8350\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5c06\u6253\u5370\u903b\u8f91\u4e0e\u4e1a\u52a1\u903b\u8f91\u5206\u79bb\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7\u88c5\u9970\u5668\u6765\u6253\u5370\u51fd\u6570\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528print\u8bed\u53e5<\/h3>\n<\/p>\n<p><p><strong>print\u8bed\u53e5<\/strong> \u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e2a\u51fd\u6570\u7684\u5f00\u5934\u548c\u7ed3\u5c3e\u6dfb\u52a0print\u8bed\u53e5\uff0c\u4ee5\u6253\u5370\u51fd\u6570\u8c03\u7528\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_function(x, y):<\/p>\n<p>    print(f&quot;Calling my_function with arguments: x={x}, y={y}&quot;)<\/p>\n<p>    result = x + y<\/p>\n<p>    print(f&quot;Result of my_function: {result}&quot;)<\/p>\n<p>    return result<\/p>\n<p>my_function(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528logging\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>logging\u6a21\u5757<\/strong> \u63d0\u4f9b\u4e86\u6bd4print\u8bed\u53e5\u66f4\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8bb0\u5f55\u4e0d\u540c\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\uff0c\u5e76\u4e14\u53ef\u4ee5\u7075\u6d3b\u5730\u914d\u7f6e\u8f93\u51fa\u683c\u5f0f\u548c\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.INFO)<\/p>\n<p>def my_function(x, y):<\/p>\n<p>    logging.info(f&quot;Calling my_function with arguments: x={x}, y={y}&quot;)<\/p>\n<p>    result = x + y<\/p>\n<p>    logging.info(f&quot;Result of my_function: {result}&quot;)<\/p>\n<p>    return result<\/p>\n<p>my_function(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u4e2a\u66f4\u4e3a\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u53ef\u4ee5\u5c06\u51fd\u6570\u8c03\u7528\u7684\u6253\u5370\u903b\u8f91\u4e0e\u4e1a\u52a1\u903b\u8f91\u5206\u79bb\uff0c\u589e\u5f3a\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def log_function_call(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Calling {func.__name__} with arguments: {args}, {kwargs}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Result of {func.__name__}: {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@log_function_call<\/p>\n<p>def my_function(x, y):<\/p>\n<p>    return x + y<\/p>\n<p>my_function(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6df1\u5165\u4ecb\u7ecd\u88c5\u9970\u5668<\/h2>\n<\/p>\n<p><h3>1\u3001\u4ec0\u4e48\u662f\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u3002\u88c5\u9970\u5668\u901a\u5e38\u7528\u4e8e\u5728\u4e0d\u4fee\u6539\u539f\u51fd\u6570\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5982\u4f55\u5b9a\u4e49\u548c\u4f7f\u7528\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u88c5\u9970\u5668\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53d7\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u7684\u51fd\u6570\uff0c\u7136\u540e\u5728\u8fd9\u4e2a\u51fd\u6570\u5185\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff08\u901a\u5e38\u79f0\u4e3awrapper\uff09\uff0c\u8fd9\u4e2a\u65b0\u7684\u51fd\u6570\u4f1a\u5728\u539f\u51fd\u6570\u8c03\u7528\u524d\u540e\u6dfb\u52a0\u989d\u5916\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        # \u5728\u8fd9\u91cc\u6dfb\u52a0\u524d\u7f6e\u529f\u80fd<\/p>\n<p>        print(f&quot;Calling {func.__name__} with arguments: {args}, {kwargs}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        # \u5728\u8fd9\u91cc\u6dfb\u52a0\u540e\u7f6e\u529f\u80fd<\/p>\n<p>        print(f&quot;Result of {func.__name__}: {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u793a\u4f8b\uff1a\u6253\u5370\u51fd\u6570\u8c03\u7528<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u5b9a\u4e49\u7684\u88c5\u9970\u5668\u6765\u6253\u5370\u51fd\u6570\u8c03\u7528\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@my_decorator<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>add(2, 3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Calling add with arguments: (2, 3), {}<\/p>\n<p>Result of add: 5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u66f4\u591a\u590d\u6742\u7684\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u6253\u5370\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7528\u9014\uff0c\u4f8b\u5982\uff1a\u6267\u884c\u65f6\u95f4\u6d4b\u91cf\u3001\u6743\u9650\u68c0\u67e5\u3001\u7f13\u5b58\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u7684\u88c5\u9970\u5668\u793a\u4f8b\uff0c\u5b83\u4e0d\u4ec5\u6253\u5370\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u8bb0\u5f55\u51fd\u6570\u6267\u884c\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import time<\/p>\n<p>def timing_decorator(func):<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        start_time = time.time()<\/p>\n<p>        print(f&quot;Calling {func.__name__} with arguments: {args}, {kwargs}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        end_time = time.time()<\/p>\n<p>        print(f&quot;Result of {func.__name__}: {result}&quot;)<\/p>\n<p>        print(f&quot;Execution time of {func.__name__}: {end_time - start_time} seconds&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@timing_decorator<\/p>\n<p>def slow_function(seconds):<\/p>\n<p>    time.sleep(seconds)<\/p>\n<p>    return f&quot;Slept for {seconds} seconds&quot;<\/p>\n<p>slow_function(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5\u3001\u7c7b\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u51fd\u6570\u88c5\u9970\u5668\uff0c\u8fd8\u6709\u4e00\u79cd\u88c5\u9970\u5668\u662f\u5e94\u7528\u4e8e\u7c7b\u7684\uff0c\u79f0\u4e3a\u7c7b\u88c5\u9970\u5668\u3002\u7c7b\u88c5\u9970\u5668\u53ef\u4ee5\u7528\u4e8e\u5728\u7c7b\u5b9e\u4f8b\u5316\u524d\u540e\u6dfb\u52a0\u989d\u5916\u7684\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def class_decorator(cls):<\/p>\n<p>    class Wrapper:<\/p>\n<p>        def __init__(self, *args, kwargs):<\/p>\n<p>            print(f&quot;Creating instance of {cls.__name__} with arguments: {args}, {kwargs}&quot;)<\/p>\n<p>            self.instance = cls(*args, kwargs)<\/p>\n<p>        def __getattr__(self, name):<\/p>\n<p>            return getattr(self.instance, name)<\/p>\n<p>    return Wrapper<\/p>\n<p>@class_decorator<\/p>\n<p>class MyClass:<\/p>\n<p>    def __init__(self, x):<\/p>\n<p>        self.x = x<\/p>\n<p>    def display(self):<\/p>\n<p>        print(f&quot;MyClass instance with x={self.x}&quot;)<\/p>\n<p>obj = MyClass(10)<\/p>\n<p>obj.display()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Creating instance of MyClass with arguments: (10,), {}<\/p>\n<p>MyClass instance with x=10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6\u3001\u88c5\u9970\u5668\u7684\u5d4c\u5957\u548c\u7ec4\u5408<\/h3>\n<\/p>\n<p><p>\u88c5\u9970\u5668\u53ef\u4ee5\u5d4c\u5957\u548c\u7ec4\u5408\u4f7f\u7528\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4eec\u975e\u5e38\u7075\u6d3b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u5e94\u7528\u4e00\u4e2a\u7528\u4e8e\u6253\u5370\u51fd\u6570\u8c03\u7528\u7684\u88c5\u9970\u5668\uff0c\u518d\u5e94\u7528\u4e00\u4e2a\u7528\u4e8e\u6d4b\u91cf\u6267\u884c\u65f6\u95f4\u7684\u88c5\u9970\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@timing_decorator<\/p>\n<p>@my_decorator<\/p>\n<p>def combined_function(x, y):<\/p>\n<p>    return x * y<\/p>\n<p>combined_function(4, 5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code>Calling combined_function with arguments: (4, 5), {}<\/p>\n<p>Calling wrapper with arguments: (4, 5), {}<\/p>\n<p>Result of wrapper: 20<\/p>\n<p>Execution time of wrapper: &lt;time&gt; seconds<\/p>\n<p>Result of combined_function: 20<\/p>\n<p>Execution time of combined_function: &lt;time&gt; seconds<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7\u3001\u4f20\u9012\u53c2\u6570\u7ed9\u88c5\u9970\u5668<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u4e3a\u88c5\u9970\u5668\u4f20\u9012\u53c2\u6570\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u518d\u5d4c\u5957\u4e00\u5c42\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def log_with_level(level):<\/p>\n<p>    def decorator(func):<\/p>\n<p>        def wrapper(*args, kwargs):<\/p>\n<p>            if level == &quot;info&quot;:<\/p>\n<p>                print(f&quot;INFO: Calling {func.__name__} with arguments: {args}, {kwargs}&quot;)<\/p>\n<p>            result = func(*args, kwargs)<\/p>\n<p>            if level == &quot;info&quot;:<\/p>\n<p>                print(f&quot;INFO: Result of {func.__name__}: {result}&quot;)<\/p>\n<p>            return result<\/p>\n<p>        return wrapper<\/p>\n<p>    return decorator<\/p>\n<p>@log_with_level(&quot;info&quot;)<\/p>\n<p>def multiply(a, b):<\/p>\n<p>    return a * b<\/p>\n<p>multiply(3, 4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8\u3001\u4f7f\u7528functools.wraps\u4fdd\u7559\u539f\u51fd\u6570\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u5728\u88c5\u9970\u5668\u4e2d\u4f7f\u7528<code>functools.wraps<\/code>\u53ef\u4ee5\u4fdd\u7559\u539f\u51fd\u6570\u7684\u540d\u79f0\u3001\u6587\u6863\u5b57\u7b26\u4e32\u7b49\u4fe1\u606f\uff0c\u8fd9\u5728\u8c03\u8bd5\u548c\u6587\u6863\u751f\u6210\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import functools<\/p>\n<p>def my_decorator(func):<\/p>\n<p>    @functools.wraps(func)<\/p>\n<p>    def wrapper(*args, kwargs):<\/p>\n<p>        print(f&quot;Calling {func.__name__} with arguments: {args}, {kwargs}&quot;)<\/p>\n<p>        result = func(*args, kwargs)<\/p>\n<p>        print(f&quot;Result of {func.__name__}: {result}&quot;)<\/p>\n<p>        return result<\/p>\n<p>    return wrapper<\/p>\n<p>@my_decorator<\/p>\n<p>def add(a, b):<\/p>\n<p>    &quot;&quot;&quot;Add two numbers.&quot;&quot;&quot;<\/p>\n<p>    return a + b<\/p>\n<p>print(add.__name__)  # \u8f93\u51fa: add<\/p>\n<p>print(add.__doc__)   # \u8f93\u51fa: Add two numbers.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9\u3001\u4f7f\u7528\u88c5\u9970\u5668\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5206\u79bb\u903b\u8f91<\/strong>\uff1a\u88c5\u9970\u5668\u5e94\u5c3d\u53ef\u80fd\u5c06\u9644\u52a0\u529f\u80fd\u4e0e\u4e1a\u52a1\u903b\u8f91\u5206\u79bb\uff0c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n<li><strong>\u4f7f\u7528functools.wraps<\/strong>\uff1a\u786e\u4fdd\u88c5\u9970\u5668\u4e0d\u4f1a\u4e22\u5931\u539f\u51fd\u6570\u7684\u4fe1\u606f\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u8bbe\u8ba1\u7075\u6d3b\u7684\u88c5\u9970\u5668\uff0c\u4f7f\u5176\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u914d\u7f6e\u884c\u4e3a\u3002<\/li>\n<li><strong>\u7ec4\u5408\u4f7f\u7528<\/strong>\uff1a\u5145\u5206\u5229\u7528\u88c5\u9970\u5668\u7684\u7ec4\u5408\u7279\u6027\uff0c\u521b\u5efa\u5f3a\u5927\u4e14\u53ef\u590d\u7528\u7684\u529f\u80fd\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u5de7\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u6253\u5370\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u6dfb\u52a0\u5176\u4ed6\u6709\u7528\u7684\u529f\u80fd\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\u8ffd\u8e2a\u51fd\u6570\u8c03\u7528\u7684\u987a\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u88c5\u9970\u5668\u6765\u6253\u5370\u51fd\u6570\u8c03\u7528\u7684\u987a\u5e8f\u3002\u521b\u5efa\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u8bb0\u5f55\u6bcf\u6b21\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u7684\u540d\u79f0\u548c\u53c2\u6570\u3002\u8fd9\u6837\uff0c\u60a8\u4e0d\u4ec5\u53ef\u4ee5\u770b\u5230\u51fd\u6570\u7684\u6267\u884c\u987a\u5e8f\uff0c\u8fd8\u80fd\u83b7\u53d6\u8f93\u5165\u53c2\u6570\u7684\u4fe1\u606f\uff0c\u4ece\u800c\u66f4\u597d\u5730\u8c03\u8bd5\u60a8\u7684\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\uff1f<\/strong><br \/>\u5229\u7528Python\u7684\u5185\u7f6e<code>logging<\/code>\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u8bb0\u5f55\u51fd\u6570\u8c03\u7528\u7684\u4fe1\u606f\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u63a7\u5236\u8f93\u51fa\u5185\u5bb9\u7684\u8be6\u7ec6\u7a0b\u5ea6\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u60a8\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u8ffd\u8e2a\u51fd\u6570\u8c03\u7528\uff0c\u800c\u4e0d\u4f1a\u5e72\u6270\u6b63\u5e38\u7684\u63a7\u5236\u53f0\u8f93\u51fa\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u663e\u793a\u51fd\u6570\u8c03\u7528\u7684\u6808\u4fe1\u606f\uff1f<\/strong><br \/>\u4f7f\u7528<code>traceback<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u6355\u83b7\u5e76\u6253\u5370\u8c03\u7528\u6808\u4fe1\u606f\u3002\u901a\u8fc7\u8c03\u7528<code>traceback.print_stack()<\/code>\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u5f53\u524d\u51fd\u6570\u7684\u8c03\u7528\u94fe\uff0c\u8fd9\u5bf9\u4e8e\u8c03\u8bd5\u6df1\u5c42\u5d4c\u5957\u7684\u51fd\u6570\u8c03\u7528\u975e\u5e38\u6709\u7528\uff0c\u6709\u52a9\u4e8e\u8bc6\u522b\u95ee\u9898\u7684\u6839\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u6253\u5370\u51fd\u6570\u8c03\u7528\uff0c\u53ef\u4ee5\u4f7f\u7528print\u8bed\u53e5\u3001logging\u6a21\u5757\u3001\u88c5\u9970\u5668\u7b49\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7print\u8bed\u53e5\u76f4\u63a5 [&hellip;]","protected":false},"author":3,"featured_media":1151386,"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\/1151376"}],"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=1151376"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151376\/revisions"}],"predecessor-version":[{"id":1151389,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1151376\/revisions\/1151389"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1151386"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1151376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1151376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1151376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}