{"id":816,"date":"2025-01-11T10:00:39","date_gmt":"2025-01-11T02:00:39","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=816"},"modified":"2025-01-11T10:12:09","modified_gmt":"2025-01-11T02:12:09","slug":"python%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e5%a4%a7%e5%85%a8%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/11\/python%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e5%a4%a7%e5%85%a8%e8%a7%a3%e6%9e%90\/","title":{"rendered":"\u5fc5\u770b\u7684Python\u5e38\u89c1\u95ee\u9898\u5927\u5168\u53ca\u89e3\u6790"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Python \u7f16\u7a0b\u8bed\u8a00\u56e0\u5176\u7b80\u6d01\u3001\u6613\u5b66\u800c\u53d7\u5230\u5e7f\u6cdb\u6b22\u8fce\uff0c\u4f46\u5728\u5b66\u4e60\u548c\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f Python \u4e2d\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u6309\u4e0d\u540c\u5206\u7c7b\u5217\u51fa\uff0c\u5e2e\u52a9\u4f60\u4e86\u89e3\u5e76\u66f4\u597d\u5730\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u8bed\u6cd5\u548c\u57fa\u7840\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1.1 <strong>IndentationError\uff08\u7f29\u8fdb\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u901a\u8fc7\u7f29\u8fdb\u6765\u6807\u8bc6\u4ee3\u7801\u5757\uff0c\u6240\u4ee5\u4efb\u4f55\u9519\u8bef\u7684\u7f29\u8fdb\u90fd\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u65e0\u6cd5\u8fd0\u884c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u4f7f\u7528\u4e00\u81f4\u7684\u7a7a\u683c\u6570\uff08\u901a\u5e38\u662f4\u4e2a\u7a7a\u683c\uff09\u6765\u8fdb\u884c\u7f29\u8fdb\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.2 <strong>NameError\uff08\u540d\u79f0\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4ee3\u7801\u4e2d\u5f15\u7528\u4e86\u4e00\u4e2a\u672a\u5b9a\u4e49\u7684\u53d8\u91cf\u3001\u51fd\u6570\u6216\u7c7b\u65f6\uff0c\u4f1a\u5f15\u53d1 <code>NameError<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u68c0\u67e5\u53d8\u91cf\u3001\u51fd\u6570\u6216\u7c7b\u7684\u62fc\u5199\u662f\u5426\u6b63\u786e\uff0c\u5e76\u786e\u4fdd\u5b83\u4eec\u5728\u4f7f\u7528\u524d\u5df2\u7ecf\u5b9a\u4e49\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.3 <strong>SyntaxError\uff08\u8bed\u6cd5\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u4ee3\u7801\u4e2d\u7684\u8bed\u6cd5\u9519\u8bef\uff0c\u5982\u6f0f\u6389\u5192\u53f7\u3001\u62ec\u53f7\u4e0d\u5339\u914d\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4ed4\u7ec6\u68c0\u67e5\u8bed\u6cd5\uff0c\u7279\u522b\u662f\u62ec\u53f7\u3001\u5f15\u53f7\u548c\u5192\u53f7\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">1.4 <strong>TypeError\uff08\u7c7b\u578b\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9519\u8bef\u7684\u7c7b\u578b\u64cd\u4f5c\uff0c\u5982\u8bd5\u56fe\u5c06\u5b57\u7b26\u4e32\u4e0e\u6574\u6570\u76f8\u52a0\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u68c0\u67e5\u64cd\u4f5c\u6570\u7684\u7c7b\u578b\uff0c\u5e76\u786e\u4fdd\u5b83\u4eec\u662f\u517c\u5bb9\u7684\u3002\u53ef\u4ee5\u4f7f\u7528 <code>type()<\/code> \u51fd\u6570\u68c0\u67e5\u53d8\u91cf\u7c7b\u578b\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u6570\u636e\u7c7b\u578b\u548c\u7ed3\u6784\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">2.1 <strong>IndexError\uff08\u7d22\u5f15\u8d85\u51fa\u8303\u56f4\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4f60\u5c1d\u8bd5\u8bbf\u95ee\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\u4e2d\u4e0d\u5b58\u5728\u7684\u7d22\u5f15\u65f6\uff0c\u4f1a\u5f15\u53d1 <code>IndexError<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u7d22\u5f15\u503c\u5728\u6709\u6548\u8303\u56f4\u5185\uff080 \u2264 index &lt; len(list)\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2.2 <strong>KeyError\uff08\u952e\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4f60\u8bbf\u95ee\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u7684\u952e\u65f6\uff0c\u4f1a\u5f15\u53d1 <code>KeyError<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>in<\/code> \u8fd0\u7b97\u7b26\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\uff0c\u6216\u8005\u4f7f\u7528 <code>.get()<\/code> \u65b9\u6cd5\u83b7\u53d6\u5b57\u5178\u4e2d\u7684\u952e\uff0c\u907f\u514d\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2.3 <strong>ValueError\uff08\u503c\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u51fd\u6570\u63a5\u6536\u5230\u7c7b\u578b\u6b63\u786e\u4f46\u503c\u4e0d\u6b63\u786e\u7684\u53c2\u6570\u65f6\u5f15\u53d1\uff0c\u4f8b\u5982\u5728\u6570\u5b57\u8f6c\u6362\u65f6\u4f20\u5165\u65e0\u6cd5\u89e3\u6790\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u4f20\u5165\u7684\u53c2\u6570\u503c\u7b26\u5408\u8981\u6c42\uff0c\u5982 <code>int('abc')<\/code> \u4f1a\u5f15\u53d1 <code>ValueError<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">2.4 <strong>Mutable Default Argument Issue\uff08\u53ef\u53d8\u9ed8\u8ba4\u53c2\u6570\u95ee\u9898\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u51fd\u6570\u5b9a\u4e49\u65f6\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u53ef\u53d8\u5bf9\u8c61\uff08\u5982\u5217\u8868\u6216\u5b57\u5178\uff09\u4f5c\u4e3a\u9ed8\u8ba4\u53c2\u6570\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u4e0d\u53ef\u53d8\u7c7b\u578b\uff08\u5982 <code>None<\/code>\uff09\u4f5c\u4e3a\u9ed8\u8ba4\u53c2\u6570\uff0c\u51fd\u6570\u5185\u90e8\u518d\u521d\u59cb\u5316\u53ef\u53d8\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  def func(arg=None):\n      if arg is None:\n          arg = &#91;]\n      arg.append(1)\n      return arg<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>\u51fd\u6570\u4e0e\u4f5c\u7528\u57df\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">3.1 <strong>UnboundLocalError\uff08\u5c40\u90e8\u53d8\u91cf\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bd5\u56fe\u5728\u51fd\u6570\u5185\u90e8\u5f15\u7528\u4e00\u4e2a\u5c40\u90e8\u53d8\u91cf\u4e4b\u524d\u4f7f\u7528\u5b83\uff0c\u6216\u8005\u4e0e\u5168\u5c40\u53d8\u91cf\u540c\u540d\u7684\u5c40\u90e8\u53d8\u91cf\u51fa\u73b0\u51b2\u7a81\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u68c0\u67e5\u53d8\u91cf\u4f5c\u7528\u57df\uff0c\u907f\u514d\u5c40\u90e8\u548c\u5168\u5c40\u53d8\u91cf\u547d\u540d\u51b2\u7a81\u3002\u53ef\u4ee5\u4f7f\u7528 <code>global<\/code> \u5173\u952e\u5b57\u6765\u5f15\u7528\u5168\u5c40\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3.2 <strong>RecursionError\uff08\u9012\u5f52\u6df1\u5ea6\u8d85\u9650\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9012\u5f52\u8c03\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u6ca1\u6709\u5408\u9002\u7684\u7ec8\u6b62\u6761\u4ef6\u6216\u7ec8\u6b62\u6761\u4ef6\u6ca1\u6709\u88ab\u6b63\u786e\u89e6\u53d1\uff0c\u4f1a\u5f15\u53d1\u9012\u5f52\u6df1\u5ea6\u8d85\u9650\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u9012\u5f52\u51fd\u6570\u6709\u6b63\u786e\u7684\u7ec8\u6b62\u6761\u4ef6\uff0c\u907f\u514d\u65e0\u9650\u9012\u5f52\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u6027\u80fd\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">4.1 <strong>\u5185\u5b58\u6cc4\u6f0f<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u7684\u5783\u573e\u56de\u6536\u673a\u5236\u901a\u5e38\u4f1a\u6e05\u7406\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u4f46\u5982\u679c\u5f15\u7528\u8ba1\u6570\u9519\u8bef\u6216\u5b58\u5728\u5faa\u73af\u5f15\u7528\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>weakref<\/code> \u6a21\u5757\u5904\u7406\u5faa\u73af\u5f15\u7528\uff0c\u6216\u8005\u786e\u4fdd\u5bf9\u8c61\u5728\u4e0d\u518d\u9700\u8981\u65f6\u53ca\u65f6\u91ca\u653e\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.2 <strong>\u6027\u80fd\u74f6\u9888<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u672c\u8eab\u7684\u6027\u80fd\u53ef\u80fd\u4f1a\u53d7\u5230\u9650\u5236\uff0c\u5c24\u5176\u662f\u5728\u5927\u6570\u636e\u91cf\u5904\u7406\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982 <code>set<\/code> \u66ff\u4ee3 <code>list<\/code>\uff09\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u6216\u8005\u8003\u8651\u4f7f\u7528 Cython\u3001NumPy \u7b49\u5e93\u4f18\u5316\u6027\u80fd\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">4.3 <strong>Global Interpreter Lock (GIL)<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u4e2d\u7684 GIL \u9650\u5236\u4e86\u591a\u7ebf\u7a0b\u7684\u5e76\u884c\u6267\u884c\uff0c\u5bfc\u81f4\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\uff0cPython \u7ebf\u7a0b\u53ef\u80fd\u65e0\u6cd5\u5b9e\u73b0\u771f\u6b63\u7684\u5e76\u884c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u591a\u8fdb\u7a0b\uff08\u5982 <code>multiprocessing<\/code> \u6a21\u5757\uff09\u4ee3\u66ff\u591a\u7ebf\u7a0b\uff0c\u6216\u8005\u4f7f\u7528\u9002\u5408\u5e76\u884c\u8ba1\u7b97\u7684\u5e93\uff0c\u5982 NumPy\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. <strong>\u6a21\u5757\u4e0e\u5e93\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">5.1 <strong>ImportError\uff08\u5bfc\u5165\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6a21\u5757\u6216\u5e93\u672a\u5b89\u88c5\u6216\u5bfc\u5165\u8def\u5f84\u9519\u8bef\u65f6\u4f1a\u5f15\u53d1\u6b64\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u6a21\u5757\u5df2\u6b63\u786e\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>pip install &lt;module_name><\/code> \u5b89\u88c5\u6240\u9700\u6a21\u5757\uff0c\u5e76\u68c0\u67e5\u5bfc\u5165\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">5.2 <strong>Circular Import\uff08\u5faa\u73af\u5bfc\u5165\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u4e24\u4e2a\u6216\u591a\u4e2a\u6a21\u5757\u76f8\u4e92\u5bfc\u5165\u65f6\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5faa\u73af\u5bfc\u5165\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u91cd\u6784\u4ee3\u7801\uff0c\u907f\u514d\u5faa\u73af\u4f9d\u8d56\u3002\u53ef\u4ee5\u5c06\u5bfc\u5165\u8bed\u53e5\u653e\u5165\u51fd\u6570\u6216\u65b9\u6cd5\u5185\u90e8\uff0c\u4ee5\u907f\u514d\u6a21\u5757\u52a0\u8f7d\u65f6\u7acb\u5373\u5bfc\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. <strong>\u5f02\u6b65\u4e0e\u5e76\u53d1\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">6.1 <strong>Asyncio \u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f02\u6b65\u7f16\u7a0b\u5e38\u5e38\u51fa\u73b0\u534f\u7a0b\u672a\u6b63\u786e\u8fd0\u884c\u6216\u4e8b\u4ef6\u5faa\u73af\u672a\u6b63\u786e\u5173\u95ed\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u4f7f\u7528 <code>async<\/code> \u548c <code>await<\/code> \u6b63\u786e\uff0c\u5e76\u901a\u8fc7 <code>asyncio.run()<\/code> \u6765\u542f\u52a8\u4e8b\u4ef6\u5faa\u73af\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">6.2 <strong>Deadlock\uff08\u6b7b\u9501\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u4e24\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u6216\u7ebf\u7a0b\u53ef\u80fd\u4e92\u76f8\u7b49\u5f85\uff0c\u9020\u6210\u6b7b\u9501\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5408\u7406\u8bbe\u8ba1\u9501\u548c\u8d44\u6e90\u5171\u4eab\u673a\u5236\uff0c\u907f\u514d\u8d44\u6e90\u4e92\u76f8\u5360\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. <strong>\u7f16\u7801\u4e0e\u89e3\u7801\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">7.1 <strong>UnicodeDecodeError\uff08Unicode \u89e3\u7801\u9519\u8bef\uff09<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u5c1d\u8bd5\u4f7f\u7528\u9519\u8bef\u7684\u7f16\u7801\u6765\u8bfb\u53d6\u5185\u5bb9\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u6b63\u786e\u7684\u6587\u4ef6\u7f16\u7801\uff08\u5982 UTF-8\uff09\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u6216\u8005\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u660e\u786e\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open('file.txt', 'r', encoding='utf-8') as file:\n      content = file.read()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. <strong>\u5f02\u5e38\u4e0e\u9519\u8bef\u5904\u7406\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">8.1 <strong>Try-Except-Else Finally \u4f7f\u7528\u4e0d\u5f53<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u6070\u5f53\u5730\u4f7f\u7528\u5f02\u5e38\u6355\u83b7\uff0c\u53ef\u80fd\u4f1a\u63a9\u76d6\u6f5c\u5728\u7684\u9519\u8bef\uff0c\u6216\u8005\u5728 <code>finally<\/code> \u4e2d\u4e0d\u6b63\u786e\u5730\u5904\u7406\u8d44\u6e90\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u7cbe\u786e\u6355\u83b7\u5f02\u5e38\u7c7b\u578b\uff0c\u4e0d\u8981\u6ee5\u7528 <code>except<\/code>\u3002\u5e76\u4e14\u786e\u4fdd\u5728 <code>finally<\/code> \u4e2d\u6b63\u786e\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  try:\n      # \u6267\u884c\u53ef\u80fd\u5f15\u53d1\u5f02\u5e38\u7684\u4ee3\u7801\n  except SomeSpecificException as e:\n      # \u5904\u7406\u7279\u5b9a\u5f02\u5e38\n  finally:\n      # \u603b\u662f\u6267\u884c\u7684\u6e05\u7406\u4ee3\u7801<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">9. <strong>\u8c03\u8bd5\u4e0e\u6d4b\u8bd5\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">9.1 <strong>\u8c03\u8bd5\u56f0\u96be<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7531\u4e8e Python \u7684\u52a8\u6001\u7c7b\u578b\u548c\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u6709\u65f6\u8c03\u8bd5\u8f83\u4e3a\u56f0\u96be\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff08\u5982 <code>pdb<\/code>\uff09\uff0c\u901a\u8fc7\u65ad\u70b9\u548c\u5355\u6b65\u6267\u884c\u6765\u6392\u67e5\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">9.2 <strong>\u5355\u5143\u6d4b\u8bd5\u4e0d\u8db3<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7f3a\u4e4f\u5355\u5143\u6d4b\u8bd5\u6216\u8005\u6d4b\u8bd5\u8986\u76d6\u4e0d\u8db3\uff0c\u53ef\u80fd\u5bfc\u81f4\u95ee\u9898\u96be\u4ee5\u53d1\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u7f16\u5199\u5168\u9762\u7684\u5355\u5143\u6d4b\u8bd5\uff0c\u4f7f\u7528 <code>unittest<\/code> \u6216 <code>pytest<\/code> \u6846\u67b6\u6765\u81ea\u52a8\u5316\u6d4b\u8bd5\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\">10. <strong>\u6587\u4ef6\u64cd\u4f5c\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">10.1 <strong>\u6587\u4ef6\u5173\u95ed\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6587\u4ef6\u64cd\u4f5c\u540e\u6ca1\u6709\u6b63\u786e\u5173\u95ed\u6587\u4ef6\uff0c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u6216\u8005\u6570\u636e\u6ca1\u6709\u6b63\u786e\u5199\u5165\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0cPython \u4f1a\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u5173\u95ed\uff0c\u907f\u514d\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  with open('file.txt', 'r') as f:\n      content = f.read()\n  # \u65e0\u9700\u663e\u5f0f\u8c03\u7528 f.close()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">10.2 <strong>\u6587\u4ef6\u6743\u9650\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5c1d\u8bd5\u8bfb\u53d6\u3001\u5199\u5165\u6216\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u6743\u9650\u4e0d\u8db3\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u7a0b\u5e8f\u6709\u6b63\u786e\u7684\u6587\u4ef6\u64cd\u4f5c\u6743\u9650\uff0c\u5e76\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002\u53ef\u4ee5\u901a\u8fc7 <code>os.access()<\/code> \u68c0\u67e5\u6587\u4ef6\u6743\u9650\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">11. <strong>\u5b57\u7b26\u4e32\u4e0e\u7f16\u7801\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">11.1 <strong>\u5b57\u7b26\u4e32\u62fc\u63a5\u6548\u7387\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5faa\u73af\u4e2d\u9891\u7e41\u62fc\u63a5\u5b57\u7b26\u4e32\u65f6\uff0c\u7531\u4e8e\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6548\u7387\u4f4e\u4e0b\uff0c\u5c24\u5176\u5728\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>join()<\/code> \u65b9\u6cd5\u4ee3\u66ff <code>+<\/code> \u6765\u62fc\u63a5\u5b57\u7b26\u4e32\u3002<code>join()<\/code> \u65b9\u6cd5\u5728\u5904\u7406\u5927\u91cf\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\u66f4\u9ad8\u6548\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u63a8\u8350\n  result = ''.join(list_of_strings)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">11.2 <strong>Unicode \u4e0e ASCII \u7f16\u7801\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u89e3\u7801\u9519\u8bef\u6216\u4e71\u7801\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\u663e\u5f0f\u6307\u5b9a\u7f16\u7801\uff08\u5982 UTF-8\uff09\uff0c\u5e76\u5904\u7406\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u8bfb\u53d6\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\n  with open('file.txt', 'r', encoding='utf-8') as f:\n      content = f.read()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">12. <strong>\u5185\u5b58\u548c\u8d44\u6e90\u7ba1\u7406\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">12.1 <strong>\u5faa\u73af\u5f15\u7528\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u7684\u5783\u573e\u56de\u6536\u673a\u5236\uff08GC\uff09\u53ef\u4ee5\u81ea\u52a8\u6e05\u7406\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u4f46\u5982\u679c\u5bf9\u8c61\u4e4b\u95f4\u5b58\u5728\u5faa\u73af\u5f15\u7528\uff08\u5982\u5bf9\u8c61 A \u5f15\u7528\u5bf9\u8c61 B\uff0cB \u5f15\u7528 A\uff09\uff0cGC \u53ef\u80fd\u65e0\u6cd5\u6e05\u7406\u5b83\u4eec\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\u5f15\u7528\uff0c\u6216\u8005\u4f7f\u7528 <code>weakref<\/code> \u6a21\u5757\u5904\u7406\u5bf9\u8c61\u5f15\u7528\uff0c\u51cf\u5c11\u5faa\u73af\u5f15\u7528\u5e26\u6765\u7684\u5185\u5b58\u6cc4\u6f0f\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">12.2 <strong>\u5168\u5c40\u53d8\u91cf\u6ee5\u7528<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u8fc7\u591a\u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7ef4\u62a4\u56f0\u96be\uff0c\u4e14\u53ef\u80fd\u5f15\u53d1\u610f\u5916\u7684\u526f\u4f5c\u7528\uff0c\u5c24\u5176\u662f\u5728\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u73af\u5883\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u7528\u5c40\u90e8\u53d8\u91cf\u6216\u5c01\u88c5\u5728\u7c7b\u4e2d\u7684\u5c5e\u6027\u6765\u7ba1\u7406\u72b6\u6001\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">13. <strong>\u591a\u7ebf\u7a0b\u4e0e\u5e76\u53d1\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">13.1 <strong>\u7ebf\u7a0b\u6b7b\u9501<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5f53\u591a\u4e2a\u7ebf\u7a0b\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u8d44\u6e90\u65f6\uff0c\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u907f\u514d\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u4f7f\u7528\u591a\u4e2a\u9501\uff0c\u8bbe\u8ba1\u826f\u597d\u7684\u9501\u7b56\u7565\uff0c\u4f7f\u7528 <code>with<\/code> \u8bed\u53e5\u7ba1\u7406\u9501\uff0c\u786e\u4fdd\u9501\u7684\u987a\u5e8f\u4e00\u81f4\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  lock1.acquire()\n  lock2.acquire()\n  try:\n      # \u5904\u7406\u5171\u4eab\u8d44\u6e90\n  finally:\n      lock1.release()\n      lock2.release()<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">13.2 <strong>\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python \u4e2d\u7684\u7ebf\u7a0b\u5728\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u82e5\u6ca1\u6709\u59a5\u5584\u5904\u7406\uff0c\u4f1a\u5f15\u53d1\u7ade\u6001\u6761\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u7ebf\u7a0b\u540c\u6b65\u673a\u5236\uff0c\u5982 <code>threading.Lock()<\/code> \u6765\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  lock = threading.Lock()\n  lock.acquire()\n  # \u5904\u7406\u5171\u4eab\u8d44\u6e90\n  lock.release()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">14. <strong>\u88c5\u9970\u5668\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">14.1 <strong>\u88c5\u9970\u5668\u53c2\u6570\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u4f7f\u7528\u88c5\u9970\u5668\u65f6\uff0c\u5982\u679c\u4e0d\u6b63\u786e\u4f20\u9012\u53c2\u6570\u6216\u4f7f\u7528\u4e0d\u5f53\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u88c5\u9970\u5668\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u786e\u4fdd\u88c5\u9970\u5668\u5185\u90e8\u6b63\u786e\u5904\u7406\u4f20\u5165\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>functools.wraps()<\/code> \u4fdd\u7559\u539f\u51fd\u6570\u7684\u4fe1\u606f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from functools import wraps\n\n  def my_decorator(func):\n      @wraps(func)\n      def wrapper(*args, **kwargs):\n          print(\"Before function call\")\n          result = func(*args, **kwargs)\n          print(\"After function call\")\n          return result\n      return wrapper<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">15. <strong>API \u548c\u7b2c\u4e09\u65b9\u5e93\u96c6\u6210\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">15.1 <strong>\u7248\u672c\u4e0d\u517c\u5bb9\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u65f6\uff0c\u4e0d\u540c\u7684\u5e93\u7248\u672c\u6216 Python \u7248\u672c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u517c\u5bb9\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>pip freeze<\/code> \u67e5\u770b\u5f53\u524d\u73af\u5883\u4e2d\u7684\u5e93\u7248\u672c\uff0c\u786e\u4fdd\u4f7f\u7528\u517c\u5bb9\u7684\u7248\u672c\uff1b\u53ef\u4ee5\u4f7f\u7528 <code>virtualenv<\/code> \u6765\u4e3a\u4e0d\u540c\u7684\u9879\u76ee\u7ba1\u7406\u4e0d\u540c\u7684\u73af\u5883\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  pip freeze &gt; requirements.txt<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">15.2 <strong>HTTP \u8bf7\u6c42\u9519\u8bef<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u8c03\u7528\u5916\u90e8 API \u65f6\uff0c\u53ef\u80fd\u9047\u5230\u8fde\u63a5\u9519\u8bef\u3001\u8d85\u65f6\u9519\u8bef\u6216\u54cd\u5e94\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>requests<\/code> \u5e93\u65f6\uff0c\u9002\u5f53\u8bbe\u7f6e\u8d85\u65f6\u53c2\u6570\u5e76\u6355\u83b7\u5f02\u5e38\uff0c\u5982 <code>requests.exceptions.RequestException<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import requests\n\n  try:\n      response = requests.get('https:\/\/example.com', timeout=5)\n      response.raise_for_status()  # \u68c0\u67e5\u54cd\u5e94\u72b6\u6001\n  except requests.exceptions.RequestException as e:\n      print(f\"Request error: {e}\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">16. <strong>\u6570\u636e\u5904\u7406\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">16.1 <strong>\u5904\u7406\u7a7a\u503c\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5904\u7406\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 <code>None<\/code>\u3001\u7a7a\u5b57\u7b26\u4e32\u3001\u7a7a\u5217\u8868\u7b49\u503c\uff0c\u8fd9\u4e9b\u503c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u903b\u8f91\u9519\u8bef\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u5728\u8fdb\u884c\u8ba1\u7b97\u6216\u64cd\u4f5c\u524d\uff0c\u8fdb\u884c\u7a7a\u503c\u68c0\u67e5\uff0c\u4f7f\u7528 <code>if var is None:<\/code> \u6216\u8005 <code>if not var:<\/code> \u6765\u68c0\u67e5\u7a7a\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  if my_var is not None:\n      # \u5904\u7406 my_var<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">16.2 <strong>NaN \u548c Infinity \u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c<code>NaN<\/code>\uff08Not a Number\uff09\u548c <code>Infinity<\/code> \u53ef\u80fd\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>math.isnan()<\/code> \u548c <code>math.isinf()<\/code> \u51fd\u6570\u68c0\u6d4b\u5e76\u5904\u7406 NaN \u6216 Infinity \u503c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import math\n  if math.isnan(x):\n      print(\"Value is NaN\")\n  if math.isinf(x):\n      print(\"Value is Infinity\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">16.3 <strong>\u65e5\u671f\u548c\u65f6\u95f4\u5904\u7406\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u65f6\u533a\u95ee\u9898\u6216\u65e5\u671f\u683c\u5f0f\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528 <code>datetime<\/code> \u6a21\u5757\u5904\u7406\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u5c3d\u91cf\u4f7f\u7528 UTC \u65f6\u95f4\u5e76\u8f6c\u5316\u4e3a\u672c\u5730\u65f6\u95f4\uff0c\u907f\u514d\u65f6\u533a\u6df7\u4e71\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from datetime import datetime\n  from pytz import timezone\n\n  now = datetime.now(timezone('UTC'))\n  local_time = now.astimezone(timezone('Asia\/Shanghai'))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">17. <strong>\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u95ee\u9898<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">17.1 <strong>\u8fc7\u957f\u7684\u51fd\u6570\u6216\u7c7b<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u6216\u7c7b\u7684\u4ee3\u7801\u8fc7\u957f\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u9075\u5faa\u51fd\u6570\u5355\u4e00\u804c\u8d23\u539f\u5219\uff0c\u5c06\u957f\u51fd\u6570\u5206\u89e3\u6210\u5c0f\u7684\u529f\u80fd\u51fd\u6570\uff1b\u5bf9\u4e8e\u7c7b\uff0c\u4e5f\u5e94\u8be5\u786e\u4fdd\u6bcf\u4e2a\u7c7b\u6709\u6e05\u6670\u7684\u804c\u8d23\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">17.2 <strong>\u786c\u7f16\u7801\u95ee\u9898<\/strong><\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801\u5e38\u91cf\u6216\u914d\u7f6e\uff0c\u53ef\u80fd\u5bfc\u81f4\u540e\u7eed\u4fee\u6539\u56f0\u96be\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u529e\u6cd5<\/strong>\uff1a\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\uff08\u5982 <code>.ini<\/code>\u3001<code>.yaml<\/code> \u6587\u4ef6\uff09\u6216\u73af\u5883\u53d8\u91cf\u6765\u5b58\u50a8\u5e38\u91cf\u548c\u914d\u7f6e\u4fe1\u606f\uff0c\u907f\u514d\u786c\u7f16\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import os\n\n  API_KEY = os.getenv('API_KEY')<\/code><\/pre>\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\">Python \u7f16\u7a0b\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\u8986\u76d6\u4e86\u4ece\u8bed\u6cd5\u57fa\u7840\u5230\u9ad8\u7ea7\u6982\u5ff5\u3001\u6027\u80fd\u4f18\u5316\u3001\u6570\u636e\u5904\u7406\u548c\u591a\u7ebf\u7a0b\u7b49\u591a\u4e2a\u65b9\u9762\u3002\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u5173\u952e\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u517b\u6210\u826f\u597d\u7684\u7f16\u7801\u4e60\u60ef\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528\u5408\u9002\u7684\u5de5\u5177\u548c\u6a21\u5757\u6765\u5e2e\u52a9\u7ba1\u7406\u5e38\u89c1\u95ee\u9898\uff08\u5982 <code>with<\/code> \u8bed\u53e5\u3001<code>functools.wraps<\/code>\u3001<code>requests<\/code> \u7b49\uff09\u3002<\/li>\n\n\n\n<li>\u5b9a\u671f\u8fdb\u884c\u4ee3\u7801\u590d\u5ba1\u548c\u4f18\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u4f60\u5c06\u80fd\u591f\u6709\u6548\u907f\u514d\u548c\u89e3\u51b3\u8fd9\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u63d0\u5347\u7f16\u7a0b\u80fd\u529b\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u7f16\u7a0b\u8bed\u8a00\u56e0\u5176\u7b80\u6d01\u3001\u6613\u5b66\u800c\u53d7\u5230\u5e7f\u6cdb\u6b22\u8fce\uff0c\u4f46\u5728\u5b66\u4e60\u548c\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u7ecf\u5e38\u4f1a\u9047\u5230\u5404\u79cd\u5e38\u89c1\u95ee\u9898\u3002\u4ee5\u4e0b\u662f [&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-816","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/816","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=816"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/816\/revisions"}],"predecessor-version":[{"id":820,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/816\/revisions\/820"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}