{"id":1024339,"date":"2024-12-30T14:13:00","date_gmt":"2024-12-30T06:13:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1024339.html"},"modified":"2024-12-30T14:13:03","modified_gmt":"2024-12-30T06:13:03","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8python3-8-4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1024339.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528python3.8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/a832b610-af4a-4e23-bda4-3826eba73eaa.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u4f7f\u7528python3.8\" \/><\/p>\n<p><p> <strong>\u8981\u4f7f\u7528Python 3.8\uff0c\u9700\u8981\u5b89\u88c5Python 3.8\u3001\u8bbe\u7f6e\u597dPython\u73af\u5883\u3001\u4f7f\u7528\u5408\u9002\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u3001\u7f16\u5199\u5e76\u8fd0\u884cPython\u4ee3\u7801\u3002<\/strong> \u5176\u4e2d\uff0c\u5b89\u88c5Python 3.8\u662f\u6700\u57fa\u7840\u7684\u6b65\u9aa4\uff0c\u7528\u6237\u9700\u8981\u786e\u4fdd\u4ecePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6b63\u786e\u7684\u7248\u672c\u3002\u63a5\u4e0b\u6765\uff0c\u8bbe\u7f6ePython\u73af\u5883\u53d8\u91cf\uff0c\u786e\u4fdd\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\u3002\u9009\u62e9\u4e00\u4e2a\u9002\u5408\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u5982PyCharm\u3001VS Code\u6216Sublime Text\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7f16\u5199\u4ee3\u7801\u7684\u6548\u7387\u3002\u6700\u540e\uff0c\u901a\u8fc7\u7f16\u5199\u5e76\u8fd0\u884cPython\u4ee3\u7801\u6765\u5b9e\u9645\u5e94\u7528Python 3.8\u7684\u5404\u9879\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Python 3.8<\/h3>\n<\/p>\n<p><h4>\u4e0b\u8f7d\u4e0e\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"https:\/\/www.python.org\/%EF%BC%89%EF%BC%8C%E5%9C%A8%E4%B8%8B%E8%BD%BD%E9%A1%B5%E9%9D%A2%E6%89%BE%E5%88%B0Python\">https:\/\/www.python.org\/\uff09\uff0c\u5728\u4e0b\u8f7d\u9875\u9762\u627e\u5230Python<\/a> 3.8\u7248\u672c\u5e76\u4e0b\u8f7d\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u52a1\u5fc5\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u4ee5\u4fbf\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u80fd\u591f\u76f4\u63a5\u4f7f\u7528Python\u3002<\/p>\n<\/p>\n<p><h4>\u9a8c\u8bc1\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1Python\u662f\u5426\u6b63\u786e\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u5b89\u88c5\u6210\u529f\uff0c\u4f60\u5e94\u8be5\u80fd\u770b\u5230\u7c7b\u4f3c\u5982\u4e0b\u7684\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">Python 3.8.x<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6ePython\u73af\u5883<\/h3>\n<\/p>\n<p><h4>\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u5c5e\u6027\u4e2d\u7684\u201c\u73af\u5883\u53d8\u91cf\u201d\u8bbe\u7f6e\uff0c\u5c06Python\u7684\u5b89\u88c5\u8def\u5f84\u6dfb\u52a0\u5230\u7cfb\u7edf\u7684PATH\u53d8\u91cf\u4e2d\u3002\u5728macOS\u548cLinux\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e2d\u7f16\u8f91\u7528\u6237\u7684<code>.bashrc<\/code>\u6216<code>.zshrc<\/code>\u6587\u4ef6\uff0c\u5c06Python\u7684\u5b89\u88c5\u8def\u5f84\u6dfb\u52a0\u5230PATH\u53d8\u91cf\u4e2d\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">export PATH=&quot;\/usr\/local\/bin\/python3.8:$PATH&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f7f\u914d\u7f6e\u751f\u6548\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source ~\/.bashrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source ~\/.zshrc<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u865a\u62df\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u865a\u62df\u73af\u5883\u53ef\u4ee5\u9694\u79bb\u9879\u76ee\u7684\u4f9d\u8d56\u5e93\uff0c\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5e93\u7248\u672c\u51b2\u7a81\u3002\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6a21\u5757\u521b\u5efa\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3.8 -m venv myenv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<p>\u5728Windows\u7cfb\u7edf\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">myenv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728macOS\u548cLinux\u7cfb\u7edf\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">source myenv\/bin\/activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u865a\u62df\u73af\u5883\u6fc0\u6d3b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff0c\u5e76\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u9009\u62e9\u5408\u9002\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668<\/h3>\n<\/p>\n<p><h4>PyCharm<\/h4>\n<\/p>\n<p><p>PyCharm\u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684Python IDE\uff0c\u652f\u6301\u4ee3\u7801\u8865\u5168\u3001\u8c03\u8bd5\u3001\u7248\u672c\u63a7\u5236\u7b49\u529f\u80fd\u3002\u53ef\u4ee5\u4eceJetBr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5PyCharm\u3002\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u521b\u5efa\u65b0\u7684Python\u9879\u76ee\uff0c\u9009\u62e9Python 3.8\u89e3\u91ca\u5668\uff0c\u5e76\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>Visual Studio Code<\/h4>\n<\/p>\n<p><p>Visual Studio Code\uff08VS Code\uff09\u662f\u4e00\u6b3e\u8f7b\u91cf\u7ea7\u7684\u4ee3\u7801\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002\u53ef\u4ee5\u4eceVS Code\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5VS Code\u3002\u5b89\u88c5Python\u6269\u5c55\u540e\uff0cVS Code\u53ef\u4ee5\u63d0\u4f9b\u4ee3\u7801\u8865\u5168\u3001\u8c03\u8bd5\u7b49\u529f\u80fd\u3002\u53ef\u4ee5\u5728VS Code\u4e2d\u521b\u5efa\u65b0\u7684Python\u6587\u4ef6\uff0c\u9009\u62e9Python 3.8\u89e3\u91ca\u5668\uff0c\u5e76\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>Sublime Text<\/h4>\n<\/p>\n<p><p>Sublime Text\u662f\u4e00\u6b3e\u6d41\u884c\u7684\u6587\u672c\u7f16\u8f91\u5668\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u3002\u53ef\u4ee5\u4eceSublime Text\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5Sublime Text\u3002\u5b89\u88c5Package Control\u540e\uff0c\u53ef\u4ee5\u5b89\u88c5Python\u76f8\u5173\u7684\u63d2\u4ef6\uff0c\u63d0\u4f9b\u4ee3\u7801\u8865\u5168\u7b49\u529f\u80fd\u3002\u53ef\u4ee5\u5728Sublime Text\u4e2d\u521b\u5efa\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7f16\u5199\u5e76\u8fd0\u884cPython\u4ee3\u7801<\/h3>\n<\/p>\n<p><h4>\u7f16\u5199\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff08\u6587\u4ef6\u6269\u5c55\u540d\u4e3a<code>.py<\/code>\uff09\uff0c\u4f8b\u5982<code>hello.py<\/code>\uff0c\u5e76\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Hello, World!&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4fdd\u5b58\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5728IDE\u4e2d\u76f4\u63a5\u8fd0\u884c\u4ee3\u7801\uff0c\u6216\u8005\u5728\u547d\u4ee4\u884c\u4e2d\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h4>\u8fd0\u884c\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\uff0c\u5bfc\u822a\u5230Python\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python3 hello.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u4e00\u5207\u6b63\u5e38\uff0c\u4f60\u5e94\u8be5\u80fd\u770b\u5230\u5982\u4e0b\u8f93\u51fa\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">Hello, World!<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b66\u4e60Python 3.8\u7684\u65b0\u7279\u6027<\/h3>\n<\/p>\n<p><h4>\u4f4d\u7f6e\u53c2\u6570\uff08Positional-only Parameters\uff09<\/h4>\n<\/p>\n<p><p>Python 3.8\u5f15\u5165\u4e86\u4f4d\u7f6e\u53c2\u6570\uff0c\u5141\u8bb8\u51fd\u6570\u53c2\u6570\u53ea\u80fd\u901a\u8fc7\u4f4d\u7f6e\u4f20\u9012\uff0c\u800c\u4e0d\u80fd\u901a\u8fc7\u5173\u952e\u5b57\u4f20\u9012\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def func(a, b, \/, c, d):<\/p>\n<p>    print(a, b, c, d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8c03\u7528<code>func<\/code>\u65f6\uff0c<code>a<\/code>\u548c<code>b<\/code>\u5fc5\u987b\u4f5c\u4e3a\u4f4d\u7f6e\u53c2\u6570\u4f20\u9012\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">func(1, 2, c=3, d=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h4>walrus\u8fd0\u7b97\u7b26\uff08:=\uff09<\/h4>\n<\/p>\n<p><p>Python 3.8\u5f15\u5165\u4e86walrus\u8fd0\u7b97\u7b26\uff0c\u5141\u8bb8\u5728\u8868\u8fbe\u5f0f\u4e2d\u8fdb\u884c\u8d4b\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if (n := len(some_list)) &gt; 10:<\/p>\n<p>    print(f&quot;List is too long ({n} elements, expected &lt;= 10)&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u7684\u5197\u4f59\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u7b80\u6d01\u6027\u3002<\/p>\n<\/p>\n<p><h4>f-string\u652f\u6301\u7b49\u53f7\uff08=\uff09\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u5728Python 3.8\u4e2d\uff0cf-string\u652f\u6301\u5728\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u7b49\u53f7\uff08=\uff09\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8c03\u8bd5\u548c\u6253\u5370\u53d8\u91cf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = 10<\/p>\n<p>print(f&quot;{x=}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">x=10<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u67e5\u770b\u53d8\u91cf\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u6539\u8fdb\u7684\u6a21\u5757\u5bfc\u5165\uff08importlib\uff09<\/h4>\n<\/p>\n<p><p>Python 3.8\u6539\u8fdb\u4e86\u6a21\u5757\u5bfc\u5165\u673a\u5236\uff0c\u63d0\u4f9b\u4e86\u66f4\u5feb\u7684\u6a21\u5757\u5bfc\u5165\u901f\u5ea6\u548c\u66f4\u7a33\u5b9a\u7684\u5bfc\u5165\u884c\u4e3a\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>importlib.resources<\/code>\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8bbf\u95ee\u5305\u5185\u7684\u8d44\u6e90\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import importlib.resources<\/p>\n<p>with importlib.resources.open_text(&#39;package_name&#39;, &#39;resource.txt&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u8bbf\u95ee\u5305\u5185\u7684\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8c03\u8bd5\u4e0e\u4f18\u5316Python\u4ee3\u7801<\/h3>\n<\/p>\n<p><h4>\u4f7f\u7528\u8c03\u8bd5\u5668<\/h4>\n<\/p>\n<p><p>\u5728IDE\u4e2d\uff0c\u901a\u5e38\u4f1a\u5185\u7f6e\u8c03\u8bd5\u5668\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u65ad\u70b9\uff0c\u9010\u6b65\u6267\u884c\u4ee3\u7801\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\uff0c\u5e2e\u52a9\u67e5\u627e\u548c\u89e3\u51b3\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u5728PyCharm\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u70b9\u51fb\u884c\u53f7\u5de6\u4fa7\u7684\u7a7a\u767d\u533a\u57df\u8bbe\u7f6e\u65ad\u70b9\uff0c\u7136\u540e\u70b9\u51fb\u8c03\u8bd5\u6309\u94ae\u5f00\u59cb\u8c03\u8bd5\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528\u65e5\u5fd7<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u65e5\u5fd7\u53ef\u4ee5\u8bb0\u5f55\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\uff0c\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u3002Python\u5185\u7f6e<code>logging<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bb0\u5f55\u65e5\u5fd7\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(level=logging.DEBUG)<\/p>\n<p>logging.debug(&quot;This is a debug message&quot;)<\/p>\n<p>logging.info(&quot;This is an info message&quot;)<\/p>\n<p>logging.warning(&quot;This is a warning message&quot;)<\/p>\n<p>logging.error(&quot;This is an error message&quot;)<\/p>\n<p>logging.critical(&quot;This is a critical message&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8bb0\u5f55\u548c\u67e5\u770b\u7a0b\u5e8f\u7684\u8fd0\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u7f16\u5199Python\u4ee3\u7801\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6280\u5de7\u6765\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u751f\u6210\u5668\uff08generator\uff09\u4ee3\u66ff\u5217\u8868\uff08list\uff09\uff0c\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u63d0\u9ad8\u8fd0\u884c\u6548\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_generator():<\/p>\n<p>    for i in range(10):<\/p>\n<p>        yield i<\/p>\n<p>for value in my_generator():<\/p>\n<p>    print(value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>timeit<\/code>\u6a21\u5757\u6765\u6d4b\u91cf\u4ee3\u7801\u7684\u6267\u884c\u65f6\u95f4\uff0c\u5e2e\u52a9\u627e\u51fa\u6027\u80fd\u74f6\u9888\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import timeit<\/p>\n<p>def my_function():<\/p>\n<p>    return sum(range(1000))<\/p>\n<p>execution_time = timeit.timeit(my_function, number=1000)<\/p>\n<p>print(f&quot;Execution time: {execution_time} seconds&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u4f18\u5316\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b66\u4e60\u8d44\u6e90\u4e0e\u793e\u533a<\/h3>\n<\/p>\n<p><h4>\u5b98\u65b9\u6587\u6863<\/h4>\n<\/p>\n<p><p>Python\u5b98\u65b9\u6587\u6863\uff08<a href=\"https:\/\/docs.python.org\/3.8\/%EF%BC%89%E6%98%AF%E5%AD%A6%E4%B9%A0Python%E7%9A%84%E6%9C%80%E4%BD%B3%E8%B5%84%E6%BA%90%EF%BC%8C%E5%8C%85%E5%90%AB%E8%AF%A6%E7%BB%86%E7%9A%84%E8%AF%AD%E8%A8%80%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C%E3%80%81%E5%BA%93%E5%8F%82%E8%80%83%E6%89%8B%E5%86%8C%E3%80%81%E6%95%99%E7%A8%8B%E7%AD%89%E5%86%85%E5%AE%B9%E3%80%82%E5%8F%AF%E4%BB%A5%E9%80%9A%E8%BF%87%E9%98%85%E8%AF%BB%E5%AE%98%E6%96%B9%E6%96%87%E6%A1%A3%EF%BC%8C%E6%B7%B1%E5%85%A5%E4%BA%86%E8%A7%A3Python%E7%9A%84%E5%90%84%E9%A1%B9%E5%8A%9F%E8%83%BD%E5%92%8C%E7%94%A8%E6%B3%95%E3%80%82\">https:\/\/docs.python.org\/3.8\/\uff09\u662f\u5b66\u4e60Python\u7684\u6700\u4f73\u8d44\u6e90\uff0c\u5305\u542b\u8be6\u7ec6\u7684\u8bed\u8a00\u53c2\u8003\u624b\u518c\u3001\u5e93\u53c2\u8003\u624b\u518c\u3001\u6559\u7a0b\u7b49\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7\u9605\u8bfb\u5b98\u65b9\u6587\u6863\uff0c\u6df1\u5165\u4e86\u89e3Python\u7684\u5404\u9879\u529f\u80fd\u548c\u7528\u6cd5\u3002<\/a><\/p>\n<\/p>\n<p><h4>\u5728\u7ebf\u6559\u7a0b<\/h4>\n<\/p>\n<p><p>\u7f51\u4e0a\u6709\u5f88\u591a\u514d\u8d39\u7684Python\u6559\u7a0b\uff0c\u53ef\u4ee5\u5e2e\u52a9\u521d\u5b66\u8005\u5feb\u901f\u5165\u95e8\u3002\u4f8b\u5982\uff0cPython\u5b98\u65b9\u6559\u7a0b\uff08<a href=\"https:\/\/docs.python.org\/3.8\/tutorial\/%EF%BC%89%E3%80%81Real\">https:\/\/docs.python.org\/3.8\/tutorial\/\uff09\u3001Real<\/a> Python\uff08<a href=\"https:\/\/realpython.com\/%EF%BC%89%E7%AD%89%EF%BC%8C%E9%83%BD%E6%98%AF%E4%B8%8D%E9%94%99%E7%9A%84%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%BA%90%E3%80%82\">https:\/\/realpython.com\/\uff09\u7b49\uff0c\u90fd\u662f\u4e0d\u9519\u7684\u5b66\u4e60\u8d44\u6e90\u3002<\/a><\/p>\n<\/p>\n<p><h4>\u4e66\u7c4d<\/h4>\n<\/p>\n<p><p>\u6709\u5f88\u591a\u4f18\u79c0\u7684Python\u4e66\u7c4d\u53ef\u4ee5\u5e2e\u52a9\u6df1\u5165\u5b66\u4e60\u548c\u638c\u63e1Python\u3002\u4f8b\u5982\uff0c\u300aPython\u7f16\u7a0b\uff1a\u4ece\u5165\u95e8\u5230\u5b9e\u8df5\u300b\u3001\u300a\u6d41\u7545\u7684Python\u300b\u3001\u300aPython Cookbook\u300b\u7b49\uff0c\u90fd\u662f\u503c\u5f97\u63a8\u8350\u7684\u4e66\u7c4d\u3002<\/p>\n<\/p>\n<p><h4>\u793e\u533a<\/h4>\n<\/p>\n<p><p>\u52a0\u5165Python\u793e\u533a\u53ef\u4ee5\u4e0e\u5176\u4ed6\u5f00\u53d1\u8005\u4ea4\u6d41\u7ecf\u9a8c\uff0c\u83b7\u53d6\u5e2e\u52a9\u3002\u53ef\u4ee5\u53c2\u52a0\u672c\u5730\u7684Python\u7528\u6237\u7ec4\uff08PUG\uff09\u6d3b\u52a8\uff0c\u52a0\u5165\u5728\u7ebf\u8bba\u575b\uff08\u5982Stack Overflow\u3001Reddit\u7684r\/Python\u7b49\uff09\uff0c\u5173\u6ce8Python\u76f8\u5173\u7684\u535a\u5ba2\u548c\u65b0\u95fb\u7f51\u7ad9\uff08\u5982Planet Python\u3001Python Weekly\u7b49\uff09\uff0c\u4fdd\u6301\u5bf9Python\u6700\u65b0\u52a8\u6001\u7684\u5173\u6ce8\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u8d44\u6e90\uff0c\u4f60\u53ef\u4ee5\u5168\u9762\u638c\u63e1Python 3.8\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u7f16\u5199\u51fa\u9ad8\u6548\u3001\u4f18\u96c5\u7684Python\u4ee3\u7801\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u5728Python\u5b66\u4e60\u548c\u5f00\u53d1\u4e2d\u53d6\u5f97\u6210\u529f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python 3.8\uff1f<\/strong><br \/>\u8981\u5f00\u59cb\u4f7f\u7528Python 3.8\uff0c\u9996\u5148\u9700\u8981\u5728\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u5b83\u3002\u4f60\u53ef\u4ee5\u8bbf\u95eePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff0c\u4e0b\u8f7d\u9002\u5408\u4f60\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python --version<\/code>\u6765\u786e\u8ba4\u5b89\u88c5\u662f\u5426\u6210\u529f\u3002\u63a5\u4e0b\u6765\uff0c\u63a8\u8350\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u5982PyCharm\u6216VSCode\u6765\u7f16\u5199\u548c\u8fd0\u884c\u4ee3\u7801\uff0c\u8fd9\u6837\u4f1a\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>Python 3.8\u6709\u54ea\u4e9b\u65b0\u7279\u6027\u548c\u6539\u8fdb\uff1f<\/strong><br \/>Python 3.8\u5f15\u5165\u4e86\u4e00\u4e9b\u6709\u8da3\u7684\u65b0\u7279\u6027\uff0c\u6bd4\u5982\u8d4b\u503c\u8868\u8fbe\u5f0f\uff08\u4e5f\u79f0\u4e3a\u201c\u6d77\u8c61\u8fd0\u7b97\u7b26\u201d\uff09\uff0c\u5b83\u5141\u8bb8\u5728\u8868\u8fbe\u5f0f\u5185\u90e8\u8fdb\u884c\u8d4b\u503c\u3002\u6b64\u5916\uff0cPython 3.8\u8fd8\u589e\u5f3a\u4e86\u7c7b\u578b\u63d0\u793a\u529f\u80fd\uff0c\u589e\u52a0\u4e86\u4f4d\u7f6e\u53c2\u6570\u548c\u5173\u952e\u5b57\u53c2\u6570\u7684\u5408\u5e76\u7b49\u529f\u80fd\u3002\u8fd9\u4e9b\u6539\u8fdb\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4e5f\u589e\u5f3a\u4e86\u7f16\u7a0b\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u89e3\u51b3Python 3.8\u4e2d\u7684\u5e38\u89c1\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python 3.8\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u8bed\u6cd5\u9519\u8bef\u3001\u7c7b\u578b\u9519\u8bef\u548c\u8fd0\u884c\u65f6\u9519\u8bef\u3002\u5efa\u8bae\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u4f7f\u7528IDE\u63d0\u4f9b\u7684\u8c03\u8bd5\u5de5\u5177\uff0c\u8fd9\u6837\u53ef\u4ee5\u5b9e\u65f6\u6355\u6349\u9519\u8bef\u5e76\u8fdb\u884c\u4fee\u6539\u3002\u5bf9\u4e8e\u7279\u5b9a\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u901a\u8fc7\u641c\u7d22\u5f15\u64ce\u67e5\u8be2\u89e3\u51b3\u65b9\u6848\uff0c\u6216\u67e5\u9605Python\u7684\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u8bba\u575b\uff0c\u4ee5\u83b7\u53d6\u8be6\u7ec6\u7684\u9519\u8bef\u5206\u6790\u548c\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7f\u7528Python 3.8\uff0c\u9700\u8981\u5b89\u88c5Python 3.8\u3001\u8bbe\u7f6e\u597dPython\u73af\u5883\u3001\u4f7f\u7528\u5408\u9002\u7684IDE\u6216\u6587\u672c\u7f16\u8f91\u5668 [&hellip;]","protected":false},"author":3,"featured_media":1024351,"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\/1024339"}],"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=1024339"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024339\/revisions"}],"predecessor-version":[{"id":1024355,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1024339\/revisions\/1024355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1024351"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1024339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1024339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1024339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}