{"id":402485,"date":"2024-02-22T00:22:12","date_gmt":"2024-02-21T16:22:12","guid":{"rendered":"https:\/\/www.idc.net\/help\/402485\/"},"modified":"2024-02-22T00:22:12","modified_gmt":"2024-02-21T16:22:12","slug":"%e7%ae%80%e4%bb%8bpython%e7%89%88%e6%9c%ac%e9%80%89%e6%8b%a9","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/402485\/","title":{"rendered":"\u7b80\u4ecbPython\u7248\u672c\u9009\u62e9"},"content":{"rendered":"<p>\u672c\u6587\u5bf9<font>Python<\/font>\u7684\u7248\u672c\u9009\u62e9\uff0cIDE\u9009\u62e9\u53ca\u7f16\u7801\u7684\u89e3\u51b3\u65b9\u6848\u8fdb\u884c\u4e86\u4e00\u756a\u8be6\u7ec6\u7684\u63cf\u8ff0\uff0c\u5b9e\u4e3a<font>Python<\/font>\u521d\u5b66\u8005\u5fc5\u8bfb\u7684<font>Python<\/font>\u5b66\u4e60\u7ecf\u9a8c\u5fc3\u5f97,\u4e00\u79cd\u65b9\u5f0f\u662f\u4e3b\u7a0b\u5e8f\u4f7f\u7528\u4e00\u95e8\u9ad8\u7ea7\u8bed\u8a00\uff0c\u6bd4\u5982C++\u8fdb\u884c\u7f16\u5199\uff0c\u7136\u540e\u5bf9\u5176\u5d4c\u5165\u4e00\u4e2a\u811a\u672c\u89e3\u91ca\u5668\uff0c\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u6267\u884c\u4e00\u4e9b\u811a\u672c\u51fd\u6570\uff1b<\/p>\n<p>\u4f46\u6b63\u5982\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6570\u5b57 23 \u800c\u65e0\u9700\u5c06\u5b83\u4e0e\u4efb\u4f55\u540d\u79f0\u7ed1\u5b9a\u4e00\u6837\uff08\u6362\u53e5\u8bdd\u8bf4\uff0c\u8c61\u51fd\u6570\u81ea\u53d8\u91cf\u4e00\u6837\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7528 lambda \u521b\u5efa\u7684\u51fd\u6570\u5bf9\u8c61\u800c\u4e0d\u7528\u5c06\u5b83\u4e0e\u4efb\u4f55\u540d\u79f0\u7ed1\u5b9a\u3002\u4e00\u4e2a\u51fd\u6570\u53ea\u662f\u6211\u4eec\u5728 <font>Python<\/font> \u4e2d\u5bf9\u5176\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u7684\u53e6\u4e00\u4e2a\u503c\u3002 <\/p>\n<p>\u6211\u4eec\u5bf9\u7b2c\u4e00\u7c7b\u5bf9\u8c61\u6240\u6267\u884c\u7684\u4e3b\u8981\u64cd\u4f5c\u662f\u5c06\u5b83\u4eec\u4f20\u9012\u7ed9 FP \u5185\u7f6e\u51fd\u6570 map()\u3001reduce() \u548c filter()\u3002\u8fd9\u4e9b\u51fd\u6570\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u63a5\u53d7\u51fd\u6570\u5bf9\u8c61\u4f5c\u4e3a\u5176\u7b2c\u4e00\u4e2a\u81ea\u53d8\u91cf\u3002 map() \u5bf9\u6307\u5b9a\u5217\u8868\u4e2d\u6bcf\u4e2a\u5bf9\u5e94\u7684\u9879\u6267\u884c\u4f20\u9012\u7684\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u7ed3\u679c\u5217\u8868\u3002<\/p>\n<p>&nbsp;reduce() \u5bf9\u6bcf\u4e2a\u540e\u7eed\u9879\u6267\u884c\u4f20\u9012\u7684\u51fd\u6570\uff0c\u8fd4\u56de\u7684\u662f\u6700\u7ec8\u7ed3\u679c\u7684\u5185\u90e8\u7d2f\u52a0\uff1b\u4f8b\u5982 reduce(lambda n,m:n*m, range(1,10)) \u610f\u5473\u7740\u201c10 \u7684\u9636\u4e58\u201d\uff08\u6362\u53e5\u8bdd\u8bf4\uff0c\u7528\u6bcf\u4e00\u9879\u4e58\u4e0a\u524d\u4e00\u6b21\u76f8\u4e58\u7684\u4e58\u79ef\uff09\u3002 #t#<\/p>\n<p>filter() \u4f7f\u7528\u4f20\u9012\u7684\u51fd\u6570\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e00\u9879\u201c\u6c42\u503c\u201d\uff0c\u7136\u540e\u8fd4\u56de\u7ecf\u8fc7\u7504\u522b\u7684\uff0c\u901a\u8fc7\u4e86\u4f20\u9012\u51fd\u6570\u6d4b\u8bd5\u7684\u9879\u7684\u5217\u8868\u3002 \u6211\u4eec\u8fd8\u7ecf\u5e38\u5c06\u51fd\u6570\u5bf9\u8c61\u4f20\u9012\u7ed9\u81ea\u5df1\u7684\u5b9a\u5236\u51fd\u6570\uff0c\u4f46\u5b83\u4eec\u901a\u5e38\u7b49\u540c\u4e8e\u4e0a\u8ff0\u5185\u7f6e\u51fd\u6570\u7684\u7ec4\u5408\u3002 <\/p>\n<p>\u901a\u8fc7\u5c06\u8fd9\u4e09\u79cd FP \u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u7ec4\u5408\uff0c\u53ef\u4ee5\u6267\u884c\u60ca\u4eba\u7684\u4e00\u7cfb\u5217\u201c\u6d41\u201d\u64cd\u4f5c\uff08\u90fd\u4e0d\u4f7f\u7528\u8bed\u53e5\uff0c\u800c\u53ea\u4f7f\u7528\u8868\u8fbe\u5f0f\uff09\u3002 <font>Python<\/font> \u4e2d\u7684\u51fd\u6570\u5faa\u73af \u66ff\u6362\u5faa\u73af\u4e0e\u66ff\u6362\u6761\u4ef6\u5757\u4e00\u6837\u7b80\u5355\u3002<\/p>\n<p>for \u53ef\u4ee5\u76f4\u63a5\u8f6c\u6362\u6210 map()\u3002\u5bf9\u4e8e\u6211\u4eec\u7684\u6761\u4ef6\u6267\u884c\uff0c\u6211\u4eec\u9700\u8981\u5c06\u8bed\u53e5\u5757\u7b80\u5316\u6210\u5355\u4e00\u51fd\u6570\u8c03\u7528\uff08\u6211\u4eec\u6b63\u9010\u6b65\u63a5\u8fd1\u901a\u5e38\u7684\u505a\u6cd5\uff09\uff1awhile \u7684\u8f6c\u6362\u4ecd\u9700\u8981 while_block() \u51fd\u6570\uff0c\u5b83\u672c\u8eab\u5305\u542b\u8bed\u53e5\u800c\u4e0d\u4ec5\u4ec5\u662f\u8868\u8fbe\u5f0f\u3002\u4f46\u6211\u4eec\u9700\u8981\u5bf9\u8be5\u51fd\u6570\u505a\u8fdb\u4e00\u6b65\u7684\u6d88\u9664\uff08\u4f8b\u5982\u5bf9\u6a21\u677f\u4e2d\u7684 if\/else \u8fdb\u884c\u77ed\u8def\uff09\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u56e0\u4e3a\u5faa\u73af\u4e3b\u4f53\uff08\u6309\u8bbe\u8ba1\uff09\u65e0\u6cd5\u66f4\u6539\u4efb\u4f55\u53d8\u91cf\u503c\uff0c\u6240\u4ee5 &lt;cond&gt; \u5f88\u96be\u7528\u5728\u4e00\u822c\u7684\u6d4b\u8bd5\u4e2d\uff0c\u4f8b\u5982 while myvar==7\uff08\u90a3\u4e48\uff0c\u5c06\u5728 while_block() \u4e2d\u4fee\u6539\u5168\u90e8\u5185\u5bb9\uff09\u3002\u6dfb\u52a0\u66f4\u6709\u7528\u6761\u4ef6\u7684\u4e00\u4e2a\u65b9\u6cd5\u662f\u8ba9 while_block() \u8fd4\u56de\u4e00\u4e2a\u66f4\u6709\u8da3\u7684\u503c\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u8fd4\u56de\u503c\u4e0e\u7ec8\u6b62\u6761\u4ef6\u8fdb\u884c\u6bd4\u8f83\u3002\u6709\u5fc5\u8981\u770b\u4e00\u4e0b\u8fd9\u4e9b\u6d88\u9664\u8bed\u53e5\u7684\u5177\u4f53\u793a\u4f8b\uff1a <\/p>\n<pre>\n \n \n <ol>\n  \n  \n  <li><span><span>#&nbsp;imperative&nbsp;version&nbsp;of&nbsp;\"echo()\"&nbsp; &nbsp;<\/span><\/span><\/li>\n  \n  \n  <li><span>def&nbsp;echo_IMP():&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>while&nbsp;1:&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span><span><font>x<\/font><span>&nbsp;=&nbsp;<span><font>raw_input<\/font><span>(\"IMP&nbsp;--&nbsp;\")&nbsp; &nbsp;<\/span><\/span><\/span><\/span><\/span><\/li>\n  \n  \n  <li><span>if&nbsp;<span><font>x<\/font><span>&nbsp;==&nbsp;'quit':&nbsp; &nbsp;<\/span><\/span><\/span><\/li>\n  \n  \n  <li><span>break&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>else&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>print&nbsp;x&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>echo_IMP()&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>#&nbsp;utility&nbsp;function&nbsp;for&nbsp;\"identity&nbsp;with&nbsp;side-effect\"&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>def&nbsp;monadic_print(x):&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>print&nbsp;x&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>return&nbsp;x&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span>&nbsp;<\/span><\/li>\n  \n  \n  <li><span>#&nbsp;FP&nbsp;version&nbsp;of&nbsp;\"echo()\"&nbsp; &nbsp;<\/span><\/li>\n  \n  \n  <li><span><span><font>echo_FP<\/font><span>&nbsp;=&nbsp;<span><font>lambda<\/font><span>:&nbsp;monadic_print(raw_input(\"FP&nbsp;--&nbsp;\"))=='quit'&nbsp;or&nbsp;echo_FP()&nbsp; &nbsp;<\/span><\/span><\/span><\/span><\/span><\/li>\n  \n  \n  <li><span>echo_FP()&nbsp;&nbsp;<\/span><\/li>\n \n \n <\/ol><\/pre>\n<p>\u6211\u4eec\u6240\u5b8c\u6210\u7684\u662f\u8bbe\u6cd5\u5c06\u6d89\u53ca I\/O\u3001\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u7684\u5c0f\u7a0b\u5e8f\u8868\u793a\u6210\u4e00\u4e2a\u5e26\u6709\u9012\u5f52\u7684\u7eaf\u8868\u8fbe\u5f0f\uff08\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u9700\u8981\uff0c\u53ef\u4ee5\u8868\u793a\u6210\u80fd\u4f20\u9012\u5230\u4efb\u4f55\u5176\u5b83\u5730\u65b9\u7684\u51fd\u6570\u5bf9\u8c61\uff09\u3002\u6211\u4eec\u7684\u786e\u4ecd\u7136\u5229\u7528\u4e86\u5b9e\u7528\u7a0b\u5e8f\u51fd\u6570 monadic_print()\uff0c\u4f46\u8fd9\u4e2a\u51fd\u6570\u662f\u5b8c\u5168\u4e00\u822c\u6027\u7684\uff0c\u53ef\u4ee5\u5728\u6211\u4eec\u4ee5\u540e\u521b\u5efa\u7684\u6bcf\u4e2a\u51fd\u6570\u7a0b\u5e8f\u8868\u8fbe\u5f0f\u4e2d\u91cd\u7528\uff08\u5b83\u662f\u4e00\u6b21\u6027\u6210\u672c\uff09\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4efb\u4f55\u5305\u542b monadic_print(x) \u7684\u8868\u8fbe\u5f0f\u6240\u6c42\u503c\u7684\u7ed3\u679c\u90fd\u662f\u76f8\u540c\u7684\uff0c\u5c31\u8c61\u5b83\u53ea\u5305\u542b x \u4e00\u6837\u3002FP\uff08\u7279\u522b\u662f Haskell\uff09\u5bf9\u4e8e\u201c\u4e0d\u6267\u884c\u4efb\u4f55\u64cd\u4f5c\uff0c\u5728\u8fdb\u7a0b\u4e2d\u6709\u526f\u4f5c\u7528\u201d\u7684\u51fd\u6570\u5177\u6709\u201c\u5355\u4e00\u4f53\u201d\u610f\u601d\u3002 <\/p>\n<p>\u3010\u7f16\u8f91\u63a8\u8350\u3011<\/p>\n<ol>\n<li>\u5982\u4f55\u4f7fPython\u5d4c\u5165C++\u5e94\u7528\u7a0b\u5e8f\uff1f <\/li>\n<li>\u6df1\u5165\u63a2\u8ba8Ruby\u4e0ePython\u8bed\u6cd5\u6bd4\u8f83<\/li>\n<li>Python\u5b66\u4e60\u8d44\u6599\u4ecb\u7ecd\u5206\u4eab<\/li>\n<li>Python\u5b66\u4e60\u7ecf\u9a8c\u8c08\uff1a\u7248\u672c\u3001IDE\u9009\u62e9\u53ca\u7f16\u7801\u89e3\u51b3\u65b9\u6848<\/li>\n<li>\u6d45\u6790Python\u7684GIL\u548c\u7ebf\u7a0b\u5b89\u5168<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5bf9Python\u7684\u7248\u672c\u9009\u62e9\uff0cIDE\u9009\u62e9\u53ca\u7f16\u7801\u7684\u89e3\u51b3\u65b9\u6848\u8fdb\u884c\u4e86\u4e00\u756a\u8be6\u7ec6\u7684\u63cf\u8ff0\uff0c\u5b9e\u4e3aPython\u521d\u5b66\u8005\u5fc5\u8bfb\u7684Py [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-402485","post","type-post","status-publish","format-standard","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/402485","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=402485"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/402485\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=402485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=402485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=402485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}