{"id":179248,"date":"2024-05-08T20:09:04","date_gmt":"2024-05-08T12:09:04","guid":{"rendered":""},"modified":"2024-05-08T20:09:21","modified_gmt":"2024-05-08T12:09:21","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-python-%e8%a6%81%e7%89%b9%e7%ab%8b%e7%8b%ac%e8%a1%8c%e5%bc%84%e5%87%ba%e4%b8%80%e4%ba%9b%e7%8b%ac%e7%89%b9%e7%9a%84%e5%85%b3%e9%94%ae%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/179248.html","title":{"rendered":"\u4e3a\u4ec0\u4e48 Python \u8981\u7279\u7acb\u72ec\u884c\u5f04\u51fa\u4e00\u4e9b\u72ec\u7279\u7684\u5173\u952e\u5b57"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063952\/4de52bfc-71b7-4c7c-b076-08e40b7b12b2.webp\" alt=\"\u4e3a\u4ec0\u4e48 Python \u8981\u7279\u7acb\u72ec\u884c\u5f04\u51fa\u4e00\u4e9b\u72ec\u7279\u7684\u5173\u952e\u5b57\" \/><\/p>\n<p><p><strong>Python\u4e4b\u6240\u4ee5\u7279\u7acb\u72ec\u884c\u5f15\u5165\u4e00\u4e9b\u72ec\u7279\u7684\u5173\u952e\u5b57\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u7ef4\u6301\u5176\u8bed\u8a00\u7684\u7b80\u6d01\u6027\u3001\u589e\u5f3a\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u548c\u4fc3\u8fdb\u7f16\u7a0b\u7684\u9ad8\u6548\u6027\u3002<\/strong> \u5176\u4e2d\uff0c\u589e\u5f3a\u4ee3\u7801\u53ef\u8bfb\u6027\u5c24\u4e3a\u91cd\u8981\u3002Python\u7684\u8bbe\u8ba1\u54f2\u5b66\u5f3a\u8c03\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u8fd9\u4fc3\u4f7fPython\u8bbe\u8ba1\u8005\u5728\u5173\u952e\u5b57\u7684\u9009\u62e9\u4e0a\u505a\u51fa\u72ec\u5230\u4e4b\u5904\u3002\u901a\u8fc7\u5f15\u5165\u5982<code>yield<\/code>\u3001<code>lambda<\/code>\u3001<code>def<\/code>\u3001<code>class<\/code>\u7b49\u5173\u952e\u5b57\uff0cPython\u65e8\u5728\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u7406\u89e3\uff0c\u540c\u65f6\u51cf\u5c11\u7f16\u7801\u7684\u590d\u6742\u5ea6\u3002\u7279\u522b\u662f<code>yield<\/code>\u5173\u952e\u5b57\uff0c\u5728\u5904\u7406\u5e8f\u5217\u751f\u6210\u65f6\u76f8\u6bd4\u4f20\u7edf\u5faa\u73af\u6216\u9012\u5f52\u65b9\u6cd5\u66f4\u4e3a\u9ad8\u6548\uff0c\u8fd9\u663e\u793a\u4e86Python\u8bbe\u8ba1\u7684\u5de7\u601d\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u589e\u5f3a\u4ee3\u7801\u53ef\u8bfb\u6027<\/h3>\n<\/p>\n<p><p>Python\u7684\u521b\u59cb\u4ebaGuido van Rossum\u4ece\u4e00\u5f00\u59cb\u5c31\u628a\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u4f5c\u4e3a\u8bbe\u8ba1\u8bed\u8a00\u7684\u9996\u8981\u76ee\u6807\u4e4b\u4e00\u3002\u72ec\u7279\u7684\u5173\u952e\u5b57\u5982<code>def<\/code>\u548c<code>class<\/code>\u7528\u4e8e\u5b9a\u4e49\u51fd\u6570\u548c\u7c7b\uff0c\u4f7f\u5f97Python\u4ee3\u7801\u770b\u8d77\u6765\u66f4\u50cf\u662f\u53ef\u9605\u8bfb\u7684\u82f1\u8bed\uff0c\u800c\u4e0d\u662f\u96be\u4ee5\u7406\u89e3\u7684\u7f16\u7a0b\u672f\u8bed\u3002\u8fd9\u79cd\u8bbe\u8ba1\u54f2\u5b66\u4f7f\u5f97\u5373\u4f7f\u662f\u521a\u521a\u5f00\u59cb\u5b66\u4e60\u7f16\u7a0b\u7684\u4eba\u4e5f\u80fd\u5feb\u901f\u7406\u89e3\u548c\u4e0a\u624bPython\u3002<\/p>\n<\/p>\n<p><h4>\u63d0\u9ad8\u521d\u5b66\u8005\u7684\u53cb\u597d\u6027<\/h4>\n<\/p>\n<p><p>\u5bf9\u521d\u5b66\u8005\u800c\u8a00\uff0cPython\u7684\u8fd9\u4e00\u7279\u6027\u6781\u5927\u964d\u4f4e\u4e86\u7f16\u7a0b\u7684\u95e8\u69db\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>def<\/code>\u5173\u952e\u5b57\u6765\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e0e\u82f1\u8bed\u4e2d\u7684\u201cdefine\u201d\u76f4\u89c2\u5bf9\u5e94\uff0c\u8ba9\u521d\u5b66\u8005\u80fd\u66f4\u5bb9\u6613\u7406\u89e3\u8fd9\u4e00\u884c\u4e3a\u7684\u542b\u4e49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4fc3\u8fdb\u7f16\u7a0b\u6548\u7387<\/h3>\n<\/p>\n<p><p>Python\u8bed\u8a00\u7684\u8bbe\u8ba1\u8fd8\u8003\u8651\u5230\u4e86\u7f16\u7a0b\u7684\u6548\u7387\u3002\u901a\u8fc7\u5f15\u5165\u5982<code>yield<\/code>\u7b49\u5173\u952e\u5b57\uff0cPython\u7b80\u5316\u4e86\u7f16\u7a0b\u6a21\u578b\uff0c\u7279\u522b\u662f\u5728\u8fed\u4ee3\u5668\u548c\u751f\u6210\u5668\u7684\u5b9e\u73b0\u4e0a\uff0c\u6781\u5927\u7684\u63d0\u9ad8\u4e86\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>\u7b80\u5316\u590d\u6742\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p><code>yield<\/code>\u5173\u952e\u5b57\u7684\u5f15\u5165\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u4ee5\u7b80\u5355\u7684\u65b9\u5f0f\u7f16\u5199\u751f\u6210\u5668\uff0c\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u9879\u5e8f\u5217\uff0c\u800c\u4e0d\u662f\u5c06\u5b83\u4eec\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5927\u6570\u636e\u96c6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u5b83\u51cf\u5c11\u4e86\u5185\u5b58\u7684\u4f7f\u7528\uff0c\u540c\u65f6\u4ee3\u7801\u7ed3\u6784\u4e5f\u66f4\u7b80\u6d01\u6e05\u6670\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001 \u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b<\/h3>\n<\/p>\n<p><p>Python\u901a\u8fc7\u5f15\u5165\u5173\u952e\u5b57\u5982<code>lambda<\/code>\uff0c\u652f\u6301\u533f\u540d\u51fd\u6570\u7684\u7f16\u5199\u3002\u8fd9\u589e\u52a0\u4e86Python\u7684\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u98ce\u683c\uff0c\u7b80\u5316\u4ee3\u7801\u5e76\u63d0\u9ad8\u4e86\u7f16\u7801\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>\u7b80\u5316\u51fd\u6570\u5b9a\u4e49<\/h4>\n<\/p>\n<p><p><code>lambda<\/code>\u8868\u8fbe\u5f0f\u4f7f\u5f97\u5728\u7f16\u5199\u9700\u8981\u7b80\u77ed\u51fd\u6570\u7684\u5730\u65b9\u66f4\u4e3a\u9ad8\u6548\uff0c\u4f8b\u5982\u5728\u6392\u5e8f\u6216\u8fc7\u6ee4\u5217\u8868\u65f6\u3002\u8fd9\u79cd\u7b80\u6d01\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\uff0c\u800c\u4e0d\u662f\u5e03\u6ee1\u4e86\u5197\u957f\u7684\u51fd\u6570\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4fc3\u4f7f\u667a\u80fd\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>Python\u901a\u8fc7<code>try<\/code>\u3001<code>except<\/code>\u3001<code>finally<\/code>\u7b49\u5173\u952e\u5b57\uff0c\u5f15\u5165\u4e86\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002\u4e0e\u5176\u4ed6\u8bed\u8a00\u76f8\u6bd4\uff0c\u8fd9\u79cd\u673a\u5236\u7684\u5f15\u5165\u4e0d\u4ec5\u8ba9\u4ee3\u7801\u66f4\u5b89\u5168\uff0c\u4e5f\u8ba9\u9519\u8bef\u5904\u7406\u53d8\u5f97\u66f4\u4e3a\u76f4\u89c2\u548c\u6613\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u7b80\u5316\u9519\u8bef\u548c\u5f02\u5e38\u7684\u7ba1\u7406<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u5173\u952e\u5b57\uff0cPython\u5141\u8bb8\u5f00\u53d1\u8005\u4ee5\u6e05\u6670\u7684\u65b9\u5f0f\u5904\u7406\u7a0b\u5e8f\u4e2d\u53ef\u80fd\u53d1\u751f\u7684\u9519\u8bef\u548c\u5f02\u5e38\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u4f7f\u5f97\u7a0b\u5e8f\u66f4\u7a33\u5b9a\uff0c\u4e5f\u51cf\u5c11\u4e86\u8c03\u8bd5\u548c\u9519\u8bef\u5b9a\u4f4d\u7684\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4fc3\u8fdb\u793e\u533a\u5171\u8bc6<\/h3>\n<\/p>\n<p><p>Python\u793e\u533a\u5bf9\u4e8e\u8bed\u8a00\u7684\u53d1\u5c55\u6709\u7740\u76f4\u63a5\u7684\u5f71\u54cd\u3002\u5728\u8bbe\u8ba1\u65b0\u7684\u5173\u952e\u5b57\u6216\u8bed\u8a00\u7279\u6027\u65f6\uff0c\u91c7\u53d6\u4e86\u793e\u533a\u5171\u8bc6\u7684\u65b9\u5f0f\u3002\u8fd9\u786e\u4fdd\u4e86Python\u8bed\u8a00\u7684\u53d1\u5c55\u4e0d\u65ad\u9002\u5e94\u5f00\u53d1\u8005\u7684\u9700\u6c42\uff0c\u540c\u65f6\u4fdd\u6301\u4e86\u8bed\u8a00\u7684\u7edf\u4e00\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h4>\u63a8\u52a8\u8bed\u8a00\u7684\u8fdb\u5316<\/h4>\n<\/p>\n<p><p>\u793e\u533a\u5171\u8bc6\u5bf9\u4e8e\u786e\u5b9a\u54ea\u4e9b\u5173\u952e\u5b57\u5e94\u8be5\u88ab\u5f15\u5165\u5230Python\u4e2d\u8d77\u5230\u4e86\u5173\u952e\u4f5c\u7528\u3002\u901a\u8fc7PEP\uff08Python Enhancement Proposals\uff09\u8fc7\u7a0b\uff0c\u793e\u533a\u6210\u5458\u53ef\u4ee5\u63d0\u51fa\u65b0\u7684\u8bed\u8a00\u7279\u6027\u548c\u6539\u8fdb\u63d0\u6848\uff0c\u7ecf\u8fc7\u8ba8\u8bba\u548c\u5ba1\u67e5\u540e\uff0c\u8fd9\u4e9b\u63d0\u6848\u53ef\u80fd\u4f1a\u88ab\u63a5\u53d7\u5e76\u6210\u4e3aPython\u7684\u4e00\u90e8\u5206\u3002\u8fd9\u4e2a\u5f00\u653e\u7684\u53d1\u5c55\u8fc7\u7a0b\u4f7f\u5f97Python\u80fd\u591f\u4e0d\u65ad\u8fdb\u5316\uff0c\u540c\u65f6\u4fdd\u6301\u5176\u6838\u5fc3\u7279\u6027\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u7684\u72ec\u7279\u5173\u952e\u5b57\u548c\u8bbe\u8ba1\u7406\u5ff5\u4fc3\u8fdb\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3001\u7f16\u5199\u53ca\u7ef4\u62a4\u7684\u6548\u7387\uff0c\u540c\u65f6\u6fc0\u53d1\u4e86\u4e00\u4e2a\u6d3b\u8dc3\u7684\u5f00\u53d1\u8005\u793e\u7fa4\uff0c\u5171\u540c\u63a8\u52a8\u7740Python\u8bed\u8a00\u7684\u53d1\u5c55\u3002\u8fd9\u4e9b\u72ec\u7279\u7684\u8bbe\u8ba1\u9009\u62e9\u4e0d\u4ec5\u4f53\u73b0\u4e86Python\u7684\u8bbe\u8ba1\u54f2\u5b66\uff0c\u4e5f\u662f\u5176\u5e7f\u6cdb\u5e94\u7528\u548c\u53d7\u6b22\u8fce\u7684\u91cd\u8981\u539f\u56e0\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4e3a\u4ec0\u4e48 Python \u4f7f\u7528\u72ec\u7279\u7684\u5173\u952e\u5b57\u6765\u533a\u5206\u81ea\u5df1\uff1f<\/strong><\/p>\n<p>Python\u7f16\u7a0b\u8bed\u8a00\u4e4b\u6240\u4ee5\u9009\u62e9\u4f7f\u7528\u4e00\u4e9b\u72ec\u7279\u7684\u5173\u952e\u5b57\uff0c\u662f\u4e3a\u4e86\u63d0\u4f9b\u66f4\u7b80\u6d01\u3001\u53ef\u8bfb\u6027\u66f4\u9ad8\u7684\u4ee3\u7801\u3002\u901a\u8fc7\u4f7f\u7528\u72ec\u7279\u7684\u5173\u952e\u5b57\uff0cPython\u5b9e\u73b0\u4e86\u4e00\u4e9b\u72ec\u7279\u7684\u8bed\u6cd5\u548c\u529f\u80fd\uff0c\u4f7f\u5f97\u7f16\u7a0b\u8fc7\u7a0b\u66f4\u52a0\u7b80\u5355\u3001\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<p><strong>Python\u4e3a\u4ec0\u4e48\u4e0d\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u5e38\u89c1\u7684\u5173\u952e\u5b57\uff1f<\/strong><\/p>\n<p>Python\u4e4b\u6240\u4ee5\u4e0d\u4f7f\u7528\u5176\u4ed6\u8bed\u8a00\u5e38\u89c1\u7684\u5173\u952e\u5b57\uff0c\u662f\u4e3a\u4e86\u907f\u514d\u4e0e\u5176\u4ed6\u8bed\u8a00\u4ea7\u751f\u51b2\u7a81\u3002\u901a\u8fc7\u4f7f\u7528\u72ec\u7279\u7684\u5173\u952e\u5b57\uff0cPython\u53ef\u4ee5\u5728\u8bed\u6cd5\u4e0a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u533a\u5206\u5f00\u6765\uff0c\u4f7f\u5f97Python\u4ee3\u7801\u66f4\u6613\u4e8e\u53ef\u8bfb\u548c\u7f16\u5199\u3002<\/p>\n<p><strong>Python\u72ec\u7279\u7684\u5173\u952e\u5b57\u6709\u54ea\u4e9b\u4f18\u70b9\uff1f<\/strong><\/p>\n<p>\u4f7f\u7528Python\u72ec\u7279\u7684\u5173\u952e\u5b57\u6709\u4ee5\u4e0b\u51e0\u4e2a\u4f18\u70b9\uff1a<\/p>\n<ol>\n<li>\n<p>\u7b80\u6d01\u6027\uff1aPython\u7684\u5173\u952e\u5b57\u901a\u5e38\u9009\u62e9\u7b80\u6d01\u660e\u4e86\u7684\u5355\u8bcd\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u6613\u8bfb\uff0c\u63d0\u9ad8\u4e86\u7f16\u7801\u6548\u7387\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ef\u8bfb\u6027\uff1aPython\u5173\u952e\u5b57\u7684\u9009\u62e9\u8003\u8651\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/p>\n<\/li>\n<li>\n<p>\u89e3\u6790\u901f\u5ea6\uff1aPython\u7684\u5173\u952e\u5b57\u8bbe\u8ba1\u7cbe\u5de7\uff0c\u4f7f\u5f97\u89e3\u6790\u5668\u80fd\u591f\u5feb\u901f\u8bc6\u522b\u5173\u952e\u5b57\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u6267\u884c\u7684\u901f\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p>\u7edf\u4e00\u6027\uff1aPython\u7684\u5173\u952e\u5b57\u4f7f\u7528\u4e00\u81f4\u7684\u89c4\u5219\uff0c\u6ca1\u6709\u592a\u591a\u7684\u4f8b\u5916\u60c5\u51b5\uff0c\u4f7f\u5f97\u4ee3\u7801\u7f16\u5199\u66f4\u52a0\u89c4\u8303\u548c\u7edf\u4e00\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u603b\u7684\u6765\u8bf4\uff0cPython\u72ec\u7279\u7684\u5173\u952e\u5b57\u662f\u5176\u8bbe\u8ba1\u54f2\u5b66\u7684\u4e00\u90e8\u5206\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3001\u53ef\u8bfb\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e4b\u6240\u4ee5\u7279\u7acb\u72ec\u884c\u5f15\u5165\u4e00\u4e9b\u72ec\u7279\u7684\u5173\u952e\u5b57\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u7ef4\u6301\u5176\u8bed\u8a00\u7684\u7b80\u6d01\u6027\u3001\u589e\u5f3a\u4ee3\u7801\u53ef\u8bfb\u6027\u3001\u548c\u4fc3\u8fdb\u7f16\u7a0b\u7684\u9ad8\u6548 [&hellip;]","protected":false},"author":3,"featured_media":179269,"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\/179248"}],"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=179248"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179248\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179269"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}