{"id":1483,"date":"2025-02-09T09:59:07","date_gmt":"2025-02-09T01:59:07","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1483"},"modified":"2025-02-09T09:59:42","modified_gmt":"2025-02-09T01:59:42","slug":"python-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%8e%e8%b0%83%e8%af%95%e6%8a%80%e5%b7%a7%ef%bc%9a%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b8%8e%e8%b0%83%e8%af%95%e5%b7%a5%e5%85%b7%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/09\/python-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%8e%e8%b0%83%e8%af%95%e6%8a%80%e5%b7%a7%ef%bc%9a%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e4%b8%8e%e8%b0%83%e8%af%95%e5%b7%a5%e5%85%b7%e4%bd%bf%e7%94%a8\/","title":{"rendered":"Python \u5f02\u5e38\u5904\u7406\u4e0e\u8c03\u8bd5\u6280\u5de7\uff1a\u9519\u8bef\u5904\u7406\u4e0e\u8c03\u8bd5\u5de5\u5177\u4f7f\u7528\u5168\u89e3\u6790"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u672c\u6559\u7a0b\u6df1\u5165\u89e3\u6790 Python \u7684\u5f02\u5e38\u5904\u7406\u548c\u8c03\u8bd5\u6280\u5de7\uff0c\u6559\u60a8\u5982\u4f55\u6355\u83b7\u548c\u5904\u7406\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5229\u7528 <code>pdb<\/code> \u8c03\u8bd5\u5de5\u5177\u5b9a\u4f4d\u4ee3\u7801\u4e2d\u7684\u95ee\u9898\uff0c\u5e76\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 IDE \u5185\u7f6e\u8c03\u8bd5\u529f\u80fd\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3002\u638c\u63e1\u8fd9\u4e9b\u8c03\u8bd5\u6280\u5de7\uff0c\u5c06\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8be6\u7ec6\u6559\u7a0b\u5185\u5bb9\uff1aPython \u5f02\u5e38\u5904\u7406\u4e0e\u8c03\u8bd5\u6280\u5de7<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u5f02\u5e38\u5904\u7406<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9519\u8bef\u5206\u7c7b\uff1a\u8bed\u6cd5\u9519\u8bef\u4e0e\u8fd0\u884c\u65f6\u9519\u8bef<\/strong><\/li>\n\n\n\n<li><strong>\u8bed\u6cd5\u9519\u8bef<\/strong>\uff1a\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u8fdd\u53cd\u4e86 Python \u7684\u8bed\u6cd5\u89c4\u5219\uff0c\u901a\u5e38\u662f\u7f16\u8bd1\u65f6\u5c31\u80fd\u53d1\u73b0\u7684\u95ee\u9898\u3002 \u793a\u4f8b\uff1a <code># \u8bed\u6cd5\u9519\u8bef\uff1a\u7f3a\u5c11\u5192\u53f7 if x == 10 print(\"x is 10\")<\/code><\/li>\n\n\n\n<li><strong>\u8fd0\u884c\u65f6\u9519\u8bef<\/strong>\uff1a\u4ee3\u7801\u8bed\u6cd5\u6b63\u786e\uff0c\u4f46\u6267\u884c\u65f6\u51fa\u73b0\u9519\u8bef\uff0c\u4f8b\u5982\u9664\u4ee5\u96f6\u3001\u6587\u4ef6\u672a\u627e\u5230\u7b49\u3002 \u793a\u4f8b\uff1a <code># \u8fd0\u884c\u65f6\u9519\u8bef\uff1a\u9664\u4ee5\u96f6 x = 10 \/ 0 # \u4f1a\u629b\u51fa ZeroDivisionError<\/code><\/li>\n\n\n\n<li><strong><code>try<\/code>\uff0c<code>except<\/code>\uff0c<code>else<\/code>\uff0c<code>finally<\/code><\/strong><br>Python \u4f7f\u7528 <code>try<\/code> \u548c <code>except<\/code> \u5757\u6765\u6355\u83b7\u548c\u5904\u7406\u5f02\u5e38\uff0c<code>else<\/code> \u548c <code>finally<\/code> \u5757\u7528\u4e8e\u8fdb\u4e00\u6b65\u7684\u63a7\u5236\u6d41\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  try:\n      x = 10 \/ 2  # \u6b63\u5e38\u60c5\u51b5\n  except ZeroDivisionError:\n      print(\"\u4e0d\u80fd\u9664\u4ee5\u96f6\")\n  else:\n      print(\"\u6ca1\u6709\u5f02\u5e38\uff0c\u7ed3\u679c\u662f\", x)  # \u5982\u679c\u6ca1\u6709\u5f02\u5e38\uff0c\u6267\u884c else \u5757\n  finally:\n      print(\"\u65e0\u8bba\u662f\u5426\u53d1\u751f\u5f02\u5e38\uff0c\u90fd\u4f1a\u6267\u884cfinally\u5757\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>try<\/code> \u5757\u4e2d\u653e\u7f6e\u53ef\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u4ee3\u7801\u3002<\/li>\n\n\n\n<li><code>except<\/code> \u5757\u6355\u83b7\u7279\u5b9a\u7684\u5f02\u5e38\u5e76\u5904\u7406\u3002<\/li>\n\n\n\n<li><code>else<\/code> \u5757\u5728\u6ca1\u6709\u5f02\u5e38\u65f6\u6267\u884c\u3002<\/li>\n\n\n\n<li><code>finally<\/code> \u5757\u59cb\u7ec8\u6267\u884c\uff0c\u5e38\u7528\u4e8e\u6e05\u7406\u8d44\u6e90\uff08\u5982\u5173\u95ed\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u7b49\uff09\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u5b9a\u4e49\u5f02\u5e38<\/strong>\uff1a\u6709\u65f6\u6211\u4eec\u9700\u8981\u81ea\u5b9a\u4e49\u5f02\u5e38\u6765\u8868\u793a\u7279\u5b9a\u7684\u9519\u8bef\u60c5\u51b5\u3002\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627f <code>Exception<\/code> \u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u5f02\u5e38\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  class CustomError(Exception):\n      def __init__(self, message):\n          self.message = message\n          super().__init__(self.message)\n\n  try:\n      raise CustomError(\"\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\")\n  except CustomError as e:\n      print(f\"\u6355\u83b7\u5230\u81ea\u5b9a\u4e49\u5f02\u5e38: {e}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>CustomError<\/code> \u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u7c7b\uff0c\u7ee7\u627f\u81ea <code>Exception<\/code>\uff0c\u5e76\u5728\u629b\u51fa\u65f6\u4f20\u9012\u4e86\u4e00\u4e2a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u8c03\u8bd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8c03\u8bd5\u662f\u6392\u67e5\u7a0b\u5e8f\u9519\u8bef\u7684\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\uff0cPython \u63d0\u4f9b\u4e86\u591a\u79cd\u8c03\u8bd5\u5de5\u5177\u548c\u6280\u5de7\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>pdb<\/code> \u8c03\u8bd5\u5de5\u5177<\/strong><br><code>pdb<\/code> \u662f Python \u81ea\u5e26\u7684\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u65ad\u70b9\uff0c\u9010\u884c\u8ddf\u8e2a\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u5e76\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import pdb\n\n  def divide(a, b):\n      pdb.set_trace()  # \u8bbe\u7f6e\u65ad\u70b9\n      return a \/ b\n\n  print(divide(10, 2))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pdb.set_trace()<\/code> \u4f1a\u5728\u6b64\u5904\u6682\u505c\u6267\u884c\uff0c\u5e76\u8fdb\u5165\u4ea4\u4e92\u5f0f\u8c03\u8bd5\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li>\u5728\u8c03\u8bd5\u6a21\u5f0f\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<ul><li><code>n<\/code>\uff1a\u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801<\/li><li><code>s<\/code>\uff1a\u8fdb\u5165\u5f53\u524d\u884c\u7684\u51fd\u6570<\/li><li><code>c<\/code>\uff1a\u7ee7\u7eed\u6267\u884c\u76f4\u5230\u4e0b\u4e00\u4e2a\u65ad\u70b9<\/li><li><code>p expression<\/code>\uff1a\u6253\u5370\u53d8\u91cf\u7684\u503c<\/li><\/ul>\u8c03\u8bd5\u65f6\uff0c\u53ef\u4ee5\u67e5\u770b\u5f53\u524d\u7684\u53d8\u91cf\u3001\u5806\u6808\u4fe1\u606f\uff0c\u5e76\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u6253\u5370\u8c03\u8bd5\u4fe1\u606f<\/strong><br>\u5728\u8c03\u8bd5\u65f6\uff0c\u6211\u4eec\u5e38\u5e38\u901a\u8fc7 <code>print()<\/code> \u51fd\u6570\u6253\u5370\u8c03\u8bd5\u4fe1\u606f\u6765\u89c2\u5bdf\u7a0b\u5e8f\u7684\u6267\u884c\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6253\u5370\u53d8\u91cf\u7684\u503c\u3001\u51fd\u6570\u8c03\u7528\u7684\u987a\u5e8f\u7b49\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  def add(a, b):\n      print(f\"a: {a}, b: {b}\")  # \u6253\u5370\u8c03\u8bd5\u4fe1\u606f\n      return a + b\n\n  print(add(3, 5))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u6253\u5370\u53d8\u91cf\u7684\u503c\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u7a0b\u5e8f\u7684\u6267\u884c\u6d41\uff0c\u5c24\u5176\u662f\u5728\u6ca1\u6709\u4f7f\u7528\u8c03\u8bd5\u5668\u65f6\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 IDE \u8c03\u8bd5\u529f\u80fd<\/strong><br>\u73b0\u4ee3 IDE\uff08\u5982 PyCharm\u3001VS Code \u7b49\uff09\u90fd\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u8c03\u8bd5\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bbe\u7f6e\u65ad\u70b9\u3001\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3001\u8ddf\u8e2a\u8c03\u7528\u6808\u7b49\u3002<\/li>\n\n\n\n<li><strong>PyCharm<\/strong>\uff1a\u63d0\u4f9b\u4e86\u56fe\u5f62\u5316\u7684\u8c03\u8bd5\u754c\u9762\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u65ad\u70b9\u3001\u67e5\u770b\u5c40\u90e8\u53d8\u91cf\u3001\u67e5\u770b\u5806\u6808\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>VS Code<\/strong>\uff1a\u652f\u6301 Python \u8c03\u8bd5\uff0c\u8bbe\u7f6e\u65ad\u70b9\u540e\uff0c\u8fd0\u884c\u8c03\u8bd5\u6a21\u5f0f\uff0c\u7a0b\u5e8f\u4f1a\u5728\u65ad\u70b9\u5904\u6682\u505c\uff0c\u53ef\u4ee5\u68c0\u67e5\u53d8\u91cf\u7684\u503c\u548c\u5806\u6808\u4fe1\u606f\u3002 \u5728 IDE \u4e2d\u8fdb\u884c\u8c03\u8bd5\u901a\u5e38\u6bd4 <code>print()<\/code> \u8c03\u8bd5\u66f4\u4e3a\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u8ba9\u4f60\u66f4\u76f4\u89c2\u5730\u67e5\u770b\u4ee3\u7801\u6267\u884c\u60c5\u51b5\u3001\u53d8\u91cf\u503c\u548c\u8c03\u7528\u6808\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u60a8\u5c06\u638c\u63e1 Python \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u5305\u62ec <code>try<\/code>\u3001<code>except<\/code>\u3001<code>else<\/code>\u3001<code>finally<\/code> \u7684\u7528\u6cd5\uff0c\u5b66\u4e60\u5982\u4f55\u81ea\u5b9a\u4e49\u5f02\u5e38\u6765\u5904\u7406\u7279\u5b9a\u7684\u9519\u8bef\u60c5\u51b5\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86 Python \u8c03\u8bd5\u5de5\u5177\uff08\u5982 <code>pdb<\/code>\uff09\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e2e\u52a9\u60a8\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u9010\u6b65\u6267\u884c\u5e76\u68c0\u67e5\u53d8\u91cf\u7684\u503c\uff0c\u4ece\u800c\u5feb\u901f\u5b9a\u4f4d\u9519\u8bef\u3002\u540c\u65f6\uff0c\u60a8\u8fd8\u53ef\u4ee5\u501f\u52a9 IDE \u7684\u8c03\u8bd5\u529f\u80fd\u8fdb\u4e00\u6b65\u63d0\u5347\u5f00\u53d1\u6548\u7387\uff0c\u89e3\u51b3\u5f00\u53d1\u4e2d\u7684\u5404\u79cd\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6559\u7a0b\u6df1\u5165\u89e3\u6790 Python \u7684\u5f02\u5e38\u5904\u7406\u548c\u8c03\u8bd5\u6280\u5de7\uff0c\u6559\u60a8\u5982\u4f55\u6355\u83b7\u548c\u5904\u7406\u5e38\u89c1\u7684\u9519\u8bef\u7c7b\u578b\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u5f02\u5e38\uff0c\u5229\u7528 p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-1483","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1483"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1483\/revisions"}],"predecessor-version":[{"id":1484,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1483\/revisions\/1484"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}