{"id":1183579,"date":"2025-01-15T19:16:15","date_gmt":"2025-01-15T11:16:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183579.html"},"modified":"2025-01-15T19:16:18","modified_gmt":"2025-01-15T11:16:18","slug":"python%e5%a6%82%e4%bd%95%e6%a3%80%e6%9f%a5%e7%a8%8b%e5%ba%8f%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1183579.html","title":{"rendered":"python\u5982\u4f55\u68c0\u67e5\u7a0b\u5e8f\u9519\u8bef"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25133256\/19110f09-dfb2-4311-8cf5-8e79b0108d42.webp\" alt=\"python\u5982\u4f55\u68c0\u67e5\u7a0b\u5e8f\u9519\u8bef\" \/><\/p>\n<p><p> <strong>Python\u68c0\u67e5\u7a0b\u5e8f\u9519\u8bef\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u8c03\u8bd5\u5668\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u5f02\u5e38\u5904\u7406\u548c\u5355\u5143\u6d4b\u8bd5\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u8c03\u8bd5\u5668\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u65f6\u89c2\u5bdf\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u627e\u5230\u5e76\u4fee\u6b63\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u8c03\u8bd5\u5668\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u9010\u884c\u68c0\u67e5\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\uff0c\u751a\u81f3\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u6682\u505c\u7a0b\u5e8f\u6267\u884c\u5e76\u68c0\u67e5\u5f53\u524d\u7684\u72b6\u6001\u3002\u8fd9\u5bf9\u4e8e\u590d\u6742\u7a0b\u5e8f\u7684\u9519\u8bef\u6392\u67e5\u7279\u522b\u6709\u7528\u3002Python\u7684\u6807\u51c6\u5e93\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>pdb<\/code>\u7684\u8c03\u8bd5\u5668\uff0c\u5b83\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u4f7f\u7528\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8c03\u8bd5\u5668<\/h3>\n<\/p>\n<p><h4>1.1 \u4f7f\u7528pdb\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>Python\u7684pdb\u6a21\u5757\u662f\u4e00\u4e2a\u5185\u7f6e\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u5355\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u5e76\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3002\u8981\u4f7f\u7528pdb\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165pdb\u6a21\u5757\u5e76\u8c03\u7528<code>pdb.set_trace()<\/code>\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pdb<\/p>\n<p>def add(a, b):<\/p>\n<p>    pdb.set_trace()  # \u8bbe\u7f6e\u65ad\u70b9<\/p>\n<p>    return a + b<\/p>\n<p>result = add(2, 3)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6267\u884c\u4e0a\u8ff0\u4ee3\u7801\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u5728\u8c03\u7528<code>pdb.set_trace()<\/code>\u7684\u5730\u65b9\u6682\u505c\uff0c\u5e76\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002\u6b64\u65f6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f93\u5165\u5404\u79cd\u8c03\u8bd5\u547d\u4ee4\u6765\u68c0\u67e5\u7a0b\u5e8f\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u5e38\u7528\u7684pdb\u547d\u4ee4<\/h4>\n<\/p>\n<ul>\n<li><code>n<\/code> (next): \u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801<\/li>\n<li><code>c<\/code> (continue): \u7ee7\u7eed\u6267\u884c\u76f4\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9<\/li>\n<li><code>s<\/code> (step): \u8fdb\u5165\u51fd\u6570\u5185\u90e8\u6267\u884c<\/li>\n<li><code>q<\/code> (quit): \u9000\u51fa\u8c03\u8bd5\u5668<\/li>\n<li><code>p<\/code> (print): \u6253\u5370\u53d8\u91cf\u7684\u503c\uff0c\u4f8b\u5982 <code>p result<\/code><\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u547d\u4ee4\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u9010\u6b65\u68c0\u67e5\u4ee3\u7801\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u627e\u51fa\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><h4>2.1 \u4f7f\u7528logging\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u65e5\u5fd7\u8bb0\u5f55\u662f\u53e6\u4e00\u79cd\u6709\u6548\u7684\u9519\u8bef\u68c0\u67e5\u65b9\u6cd5\u3002\u901a\u8fc7\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u65e5\u5fd7\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u51fa\u73b0\u95ee\u9898\u65f6\u5feb\u901f\u5b9a\u4f4d\u9519\u8bef\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u65e5\u5fd7\u8bb0\u5f55\u6a21\u5757<code>logging<\/code>\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.DEBUG, format=&#39;%(asctime)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p>def divide(a, b):<\/p>\n<p>    if b == 0:<\/p>\n<p>        logging.error(&quot;Division by zero&quot;)<\/p>\n<p>        return None<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 0)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u53d1\u751f\u9664\u4ee5\u96f6\u7684\u60c5\u51b5\uff0c\u65e5\u5fd7\u8bb0\u5f55\u4f1a\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u914d\u7f6e\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><p><code>logging<\/code>\u6a21\u5757\u5141\u8bb8\u5f00\u53d1\u8005\u914d\u7f6e\u4e0d\u540c\u7684\u65e5\u5fd7\u7ea7\u522b\uff08DEBUG\u3001INFO\u3001WARNING\u3001ERROR\u3001CRITICAL\uff09\u548c\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u65e5\u5fd7\u8bb0\u5f55\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">logging.basicConfig(filename=&#39;app.log&#39;, filemode=&#39;w&#39;, level=logging.DEBUG, format=&#39;%(name)s - %(levelname)s - %(message)s&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528try-except\u8bed\u53e5<\/h4>\n<\/p>\n<p><p>\u5f02\u5e38\u5904\u7406\u662f\u6355\u83b7\u548c\u5904\u7406\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u4ee5\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002Python\u63d0\u4f9b\u4e86<code>try-except<\/code>\u8bed\u53e5\u6765\u5904\u7406\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    try:<\/p>\n<p>        result = a \/ b<\/p>\n<p>    except ZeroDivisionError as e:<\/p>\n<p>        print(f&quot;Error: {e}&quot;)<\/p>\n<p>        result = None<\/p>\n<p>    return result<\/p>\n<p>result = divide(10, 0)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5982\u679c\u53d1\u751f\u9664\u4ee5\u96f6\u7684\u60c5\u51b5\uff0c<code>except<\/code>\u5757\u4f1a\u6355\u83b7<code>ZeroDivisionError<\/code>\u5f02\u5e38\uff0c\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u6355\u83b7\u591a\u4e2a\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a<code>except<\/code>\u5757\uff0c\u6216\u8005\u5728\u4e00\u4e2a<code>except<\/code>\u5757\u4e2d\u6355\u83b7\u591a\u4e2a\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    result = int(&#39;a&#39;) \/ 0<\/p>\n<p>except (ValueError, ZeroDivisionError) as e:<\/p>\n<p>    print(f&quot;Error: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u5904\u7406\u5404\u79cd\u5f02\u5e38\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5355\u5143\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528unittest\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u5355\u5143\u6d4b\u8bd5\u662f\u9a8c\u8bc1\u4ee3\u7801\u6b63\u786e\u6027\u7684\u91cd\u8981\u65b9\u6cd5\u3002Python\u7684<code>unittest<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5b8c\u6574\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u53ef\u4ee5\u7f16\u5199\u548c\u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unittest<\/p>\n<p>def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>class TestMathFunctions(unittest.TestCase):<\/p>\n<p>    def test_add(self):<\/p>\n<p>        self.assertEqual(add(2, 3), 5)<\/p>\n<p>        self.assertEqual(add(-1, 1), 0)<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    unittest.main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u7c7b<code>TestMathFunctions<\/code>\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u6d4b\u8bd5\u65b9\u6cd5<code>test_add<\/code>\uff0c\u7528\u6765\u9a8c\u8bc1<code>add<\/code>\u51fd\u6570\u7684\u6b63\u786e\u6027\u3002\u8fd0\u884c\u8be5\u6d4b\u8bd5\u65f6\uff0c<code>unittest<\/code>\u4f1a\u81ea\u52a8\u6267\u884c\u6d4b\u8bd5\u65b9\u6cd5\uff0c\u5e76\u62a5\u544a\u6d4b\u8bd5\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u4f7f\u7528pytest\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>unittest<\/code>\u6a21\u5757\uff0c<code>pytest<\/code>\u4e5f\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u6d4b\u8bd5\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u66f4\u5f3a\u5927\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    return a + b<\/p>\n<p>def test_add():<\/p>\n<p>    assert add(2, 3) == 5<\/p>\n<p>    assert add(-1, 1) == 0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u51fd\u6570<code>test_add<\/code>\uff0c\u7528\u6765\u9a8c\u8bc1<code>add<\/code>\u51fd\u6570\u7684\u6b63\u786e\u6027\u3002\u8fd0\u884c<code>pytest<\/code>\u65f6\uff0c\u5b83\u4f1a\u81ea\u52a8\u53d1\u73b0\u5e76\u6267\u884c\u6240\u6709\u4ee5<code>test_<\/code>\u5f00\u5934\u7684\u6d4b\u8bd5\u51fd\u6570\uff0c\u5e76\u62a5\u544a\u6d4b\u8bd5\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4ee3\u7801\u68c0\u67e5\u5de5\u5177<\/h3>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528pylint<\/h4>\n<\/p>\n<p><p><code>pylint<\/code>\u662f\u4e00\u4e2a\u9759\u6001\u4ee3\u7801\u5206\u6790\u5de5\u5177\uff0c\u53ef\u4ee5\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3001\u6837\u5f0f\u95ee\u9898\u548c\u6f5c\u5728\u7684\u9519\u8bef\u3002\u4f7f\u7528<code>pylint<\/code>\u53ef\u4ee5\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u5c31\u53d1\u73b0\u5e76\u4fee\u6b63\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pylint my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u65f6\uff0c<code>pylint<\/code>\u4f1a\u5206\u6790<code>my_script.py<\/code>\u6587\u4ef6\uff0c\u5e76\u62a5\u544a\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528flake8<\/h4>\n<\/p>\n<p><p><code>flake8<\/code>\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\uff0c\u7ed3\u5408\u4e86<code>pyflakes<\/code>\u3001<code>pycodestyle<\/code>\u548c<code>mccabe<\/code>\uff0c\u53ef\u4ee5\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3001\u6837\u5f0f\u95ee\u9898\u548c\u590d\u6742\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">flake8 my_script.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u65f6\uff0c<code>flake8<\/code>\u4f1a\u5206\u6790<code>my_script.py<\/code>\u6587\u4ef6\uff0c\u5e76\u62a5\u544a\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4ee3\u7801\u8986\u76d6\u7387<\/h3>\n<\/p>\n<p><h4>6.1 \u4f7f\u7528coverage\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u4ee3\u7801\u8986\u76d6\u7387\u662f\u8861\u91cf\u4ee3\u7801\u6d4b\u8bd5\u7a0b\u5ea6\u7684\u91cd\u8981\u6307\u6807\u3002<code>coverage<\/code>\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6d4b\u91cf\u6d4b\u8bd5\u4ee3\u7801\u7684\u8986\u76d6\u7387\uff0c\u627e\u51fa\u672a\u88ab\u6d4b\u8bd5\u7684\u4ee3\u7801\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">coverage run -m unittest discover<\/p>\n<p>coverage report<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u547d\u4ee4\u65f6\uff0c<code>coverage<\/code>\u4f1a\u8fd0\u884c\u6240\u6709\u5355\u5143\u6d4b\u8bd5\uff0c\u5e76\u751f\u6210\u4ee3\u7801\u8986\u76d6\u7387\u62a5\u544a\uff0c\u663e\u793a\u6bcf\u4e2a\u6587\u4ef6\u7684\u8986\u76d6\u7387\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u751f\u6210\u8986\u76d6\u7387\u62a5\u544a<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u5728\u7ec8\u7aef\u4e2d\u67e5\u770b\u8986\u76d6\u7387\u62a5\u544a\uff0c\u8fd8\u53ef\u4ee5\u751f\u6210HTML\u683c\u5f0f\u7684\u62a5\u544a\uff0c\u4fbf\u4e8e\u67e5\u770b\u548c\u5206\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">coverage html<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u751f\u6210\u7684HTML\u62a5\u544a\u4f1a\u5305\u542b\u6bcf\u4e2a\u6587\u4ef6\u7684\u8be6\u7ec6\u8986\u76d6\u7387\u4fe1\u606f\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u67e5\u770b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4ee3\u7801\u8c03\u8bd5\u6280\u5de7<\/h3>\n<\/p>\n<p><h4>7.1 \u6253\u5370\u8c03\u8bd5\u4fe1\u606f<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u8bd5\u4ee3\u7801\u65f6\uff0c\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>print<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def add(a, b):<\/p>\n<p>    print(f&quot;Adding {a} and {b}&quot;)<\/p>\n<p>    return a + b<\/p>\n<p>result = add(2, 3)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136<code>print<\/code>\u8c03\u8bd5\u6cd5\u7b80\u5355\u6709\u6548\uff0c\u4f46\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u4e0d\u5b9c\u4f7f\u7528\u8fc7\u591a\uff0c\u5e94\u5c3d\u91cf\u4f7f\u7528\u8c03\u8bd5\u5668\u548c\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n<\/p>\n<p><h4>7.2 \u4f7f\u7528assert\u8bed\u53e5<\/h4>\n<\/p>\n<p><p><code>assert<\/code>\u8bed\u53e5\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u8c03\u8bd5\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u68c0\u67e5\u6761\u4ef6\u662f\u5426\u6210\u7acb\u3002\u5982\u679c\u6761\u4ef6\u4e0d\u6210\u7acb\uff0c<code>assert<\/code>\u4f1a\u5f15\u53d1<code>AssertionError<\/code>\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def divide(a, b):<\/p>\n<p>    assert b != 0, &quot;Division by zero&quot;<\/p>\n<p>    return a \/ b<\/p>\n<p>result = divide(10, 0)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u5982\u679c<code>b<\/code>\u4e3a\u96f6\uff0c<code>assert<\/code>\u4f1a\u5f15\u53d1\u5f02\u5e38\u5e76\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u8c03\u8bd5\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><h4>8.1 \u67e5\u770b\u6587\u6863\u548c\u6e90\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u65f6\uff0c\u5982\u679c\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u67e5\u770b\u5e93\u7684\u6587\u6863\u548c\u6e90\u7801\u3002\u5927\u591a\u6570\u7b2c\u4e09\u65b9\u5e93\u90fd\u6709\u8be6\u7ec6\u7684\u6587\u6863\uff0c\u4ecb\u7ecd\u5e93\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u5e38\u89c1\u95ee\u9898\u3002\u53e6\u5916\uff0c\u67e5\u770b\u6e90\u7801\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u7406\u89e3\u5e93\u7684\u5185\u90e8\u5b9e\u73b0\uff0c\u627e\u51fa\u95ee\u9898\u7684\u6839\u6e90\u3002<\/p>\n<\/p>\n<p><h4>8.2 \u63d0\u4ea4\u95ee\u9898\u548c\u8d21\u732e\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u53d1\u73b0\u7b2c\u4e09\u65b9\u5e93\u4e2d\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u5728\u5e93\u7684GitHub\u4ed3\u5e93\u4e2d\u63d0\u4ea4\u95ee\u9898\uff08Issue\uff09\uff0c\u63cf\u8ff0\u95ee\u9898\u7684\u8be6\u7ec6\u60c5\u51b5\uff0c\u5e76\u9644\u4e0a\u76f8\u5173\u4ee3\u7801\u548c\u9519\u8bef\u4fe1\u606f\u3002\u8bb8\u591a\u5f00\u6e90\u9879\u76ee\u7684\u7ef4\u62a4\u8005\u4f1a\u79ef\u6781\u56de\u5e94\u5e76\u4fee\u590d\u95ee\u9898\u3002\u6b64\u5916\uff0c\u5982\u679c\u6709\u80fd\u529b\uff0c\u53ef\u4ee5\u4e3a\u5f00\u6e90\u9879\u76ee\u8d21\u732e\u4ee3\u7801\uff0c\u5e2e\u52a9\u4fee\u590d\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u68c0\u67e5\u7a0b\u5e8f\u9519\u8bef\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u8c03\u8bd5\u5668\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u5f02\u5e38\u5904\u7406\u548c\u5355\u5143\u6d4b\u8bd5\u3002<\/strong> \u4f7f\u7528\u8c03\u8bd5\u5668\u53ef\u4ee5\u5b9e\u65f6\u89c2\u5bdf\u7a0b\u5e8f\u7684\u8fd0\u884c\u72b6\u6001\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u9010\u884c\u68c0\u67e5\u4ee3\u7801\u3002\u65e5\u5fd7\u8bb0\u5f55\u53ef\u4ee5\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u4fe1\u606f\uff0c\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002\u5f02\u5e38\u5904\u7406\u53ef\u4ee5\u6355\u83b7\u548c\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u9632\u6b62\u7a0b\u5e8f\u5d29\u6e83\u3002\u5355\u5143\u6d4b\u8bd5\u53ef\u4ee5\u9a8c\u8bc1\u4ee3\u7801\u7684\u6b63\u786e\u6027\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u3002\u6b64\u5916\uff0c\u4ee3\u7801\u68c0\u67e5\u5de5\u5177\u548c\u4ee3\u7801\u8986\u76d6\u7387\u6d4b\u91cf\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u53d1\u73b0\u548c\u4fee\u6b63\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\u3002\u901a\u8fc7\u7efc\u5408\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u68c0\u67e5\u548c\u4fee\u590dPython\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u6355\u6349\u548c\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528try&#8230;except\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u5730\u6355\u6349\u548c\u5904\u7406\u5f02\u5e38\u3002\u4f60\u53ef\u4ee5\u5c06\u53ef\u80fd\u629b\u51fa\u9519\u8bef\u7684\u4ee3\u7801\u653e\u5728try\u5757\u4e2d\uff0c\u800c\u5728except\u5757\u4e2d\u5904\u7406\u7279\u5b9a\u7c7b\u578b\u7684\u5f02\u5e38\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u800c\u662f\u53ef\u4ee5\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u548c\u53cd\u9988\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\uff1f<\/strong><br \/>Python\u4e2d\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\u5305\u62ecSyntaxError\uff08\u8bed\u6cd5\u9519\u8bef\uff09\u3001TypeError\uff08\u7c7b\u578b\u9519\u8bef\uff09\u3001ValueError\uff08\u503c\u9519\u8bef\uff09\u548cIndexError\uff08\u7d22\u5f15\u9519\u8bef\uff09\u7b49\u3002\u4e86\u89e3\u8fd9\u4e9b\u9519\u8bef\u7c7b\u578b\u6709\u52a9\u4e8e\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\uff0c\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\u8fdb\u884c\u4fee\u590d\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u7684\u5185\u7f6e\u8c03\u8bd5\u5de5\u5177\u6765\u67e5\u627e\u9519\u8bef\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684pdb\u6a21\u5757\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u8fdb\u884c\u8c03\u8bd5\u3002\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u63d2\u5165<code>import pdb; pdb.set_trace()<\/code>\uff0c\u4f60\u53ef\u4ee5\u5728\u8be5\u884c\u5904\u6682\u505c\u6267\u884c\uff0c\u5e76\u8fdb\u5165\u8c03\u8bd5\u6a21\u5f0f\u3002\u8fd9\u5141\u8bb8\u4f60\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u4ee5\u53ca\u8ddf\u8e2a\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u53d1\u73b0\u548c\u89e3\u51b3\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u68c0\u67e5\u7a0b\u5e8f\u9519\u8bef\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u8c03\u8bd5\u5668\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u5f02\u5e38\u5904\u7406\u548c\u5355\u5143\u6d4b\u8bd5\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u8c03\u8bd5\u5668\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1183584,"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\/1183579"}],"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=1183579"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183579\/revisions"}],"predecessor-version":[{"id":1183586,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183579\/revisions\/1183586"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183584"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}