{"id":8817,"date":"2023-08-10T17:50:31","date_gmt":"2023-08-10T09:50:31","guid":{"rendered":"\/?p=8817"},"modified":"2023-08-10T17:51:24","modified_gmt":"2023-08-10T09:51:24","slug":"python%e4%b8%ad%e7%9a%84%e5%81%8f%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"\/?p=8817","title":{"rendered":"Python\u4e2d\u7684\u504f\u51fd\u6570"},"content":{"rendered":"<p>\u504f\u51fd\u6570\uff08Partial function\uff09\u662fPython\u7684functools\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u529f\u80fd\u3002<\/p>\n<p>\u7b80\u5355\u603b\u7ed3functools.partial\u7684\u4f5c\u7528\u5c31\u662f\uff0c\u628a\u4e00\u4e2a\u51fd\u6570\u7684\u67d0\u4e9b\u53c2\u6570\u7ed9\u56fa\u5b9a\u4f4f\uff08\u4e5f\u5c31\u662f\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff09\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8c03\u7528\u8fd9\u4e2a\u65b0\u51fd\u6570\u4f1a\u66f4\u7b80\u5355\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0cint()\u51fd\u6570\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u5f53\u4ec5\u4f20\u5165\u5b57\u7b26\u4e32\u65f6\uff0cint()\u51fd\u6570\u9ed8\u8ba4\u6309\u5341\u8fdb\u5236\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; int(&#039;12345&#039;)\n12345\n<\/code><\/pre>\n<p>\u4f46int()\u51fd\u6570\u8fd8\u63d0\u4f9b\u989d\u5916\u7684base\u53c2\u6570\uff0c\u9ed8\u8ba4\u503c\u4e3a10\u3002\u5982\u679c\u4f20\u5165base\u53c2\u6570\uff0c\u5c31\u53ef\u4ee5\u505aN\u8fdb\u5236\u7684\u8f6c\u6362\uff1a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; int(&#039;12345&#039;, base=8)\n5349\n>&gt;&gt; int(&#039;12345&#039;, 16)\n74565\n<\/code><\/pre>\n<p>\u5047\u8bbe\u8981\u8f6c\u6362\u5927\u91cf\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u6bcf\u6b21\u90fd\u4f20\u5165<code>int(x, base=2)<\/code>\u975e\u5e38\u9ebb\u70e6\uff0c\u4e8e\u662f\uff0c\u6211\u4eec\u60f3\u5230\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aint2()\u7684\u51fd\u6570\uff0c\u9ed8\u8ba4\u628abase=2\u4f20\u8fdb\u53bb\uff1a<\/p>\n<pre><code class=\"language-python\">def int2(x, base=2):\n    return int(x, base)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u6211\u4eec\u8f6c\u6362\u4e8c\u8fdb\u5236\u5c31\u975e\u5e38\u65b9\u4fbf\u4e86\uff1a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; int2(&#039;1000000&#039;)\n64\n>&gt;&gt; int2(&#039;1010101&#039;)\n85\n<\/code><\/pre>\n<p>functools.partial\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u504f\u51fd\u6570\u7684\uff0c\u4e0d\u9700\u8981\u6211\u4eec\u81ea\u5df1\u5b9a\u4e49int2()\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570int2\uff1a<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; import functools\n>&gt;&gt; int2 = functools.partial(int, base=2)\n>&gt;&gt; int2(&#039;1000000&#039;)\n64\n>&gt;&gt; int2(&#039;1010101&#039;)\n85\n<\/code><\/pre>\n<p>\u6ce8\u610f\u5230\u4e0a\u9762\u7684\u65b0\u7684int2\u51fd\u6570\uff0c\u4ec5\u4ec5\u662f\u628abase\u53c2\u6570\u91cd\u65b0\u8bbe\u5b9a\u9ed8\u8ba4\u503c\u4e3a2\uff0c\u4f46\u4e5f\u53ef\u4ee5\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4f20\u5165\u5176\u4ed6\u503c<\/p>\n<pre><code class=\"language-python\">>&gt;&gt; int2(&#039;1000000&#039;, base=10)\n1000000\n<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u521b\u5efa\u504f\u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u53ef\u4ee5\u63a5\u6536\u51fd\u6570\u5bf9\u8c61\u3001<code>*args<\/code>\u548c<code>**kw<\/code>\u8fd93\u4e2a\u53c2\u6570\uff0c\u6240\u4ee5\u5f53\u4f20\u5165\uff1a<\/p>\n<pre><code class=\"language-python\">int2 = functools.partial(int, base=2)\nint2(&#039;10010&#039;)\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u504f\u51fd\u6570\u5b9a\u4e49\u5904\u6709base\u5173\u952e\u5b57\uff0c\u6240\u4ee5\u662f\u4ee5<code>**kw<\/code>\u4f20\u5165\u7684\uff0c\u5b9e\u9645\u4e0a\u662f\uff1a<\/p>\n<pre><code class=\"language-python\">kw = { &#039;base&#039;: 2 }\nint(&#039;10010&#039;, **kw)<\/code><\/pre>\n<p>\u540c\u7406\uff0c\u5f53\u4f20\u5165\uff1a<\/p>\n<pre><code class=\"language-python\">max2 = functools.partial(max, 10)\nmax2(5, 6, 7)\n<\/code><\/pre>\n<p>\u56e0\u4e3a\u504f\u51fd\u6570\u5b9a\u4e49\u5904\u6ca1\u6709\u5173\u952e\u5b57\uff0c\u6240\u4ee5\u662f\u4ee5<code>*arg<\/code>\u4f20\u5165\u7684\uff0c\u5b9e\u9645\u4e0a\u662f\uff08\u628a10\u4f5c\u4e3a<code>*args<\/code>\u7684\u4e00\u90e8\u5206\u81ea\u52a8\u52a0\u5230\u5de6\u8fb9\uff09<\/p>\n<pre><code class=\"language-python\">args = (10, 5, 6, 7)\nmax(*args)\n<\/code><\/pre>\n<p>\u6240\u4ee5\uff0c\u5f53\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570\u592a\u591a\uff0c\u9700\u8981\u7b80\u5316\u65f6\uff0c\u4f7f\u7528functools.partial\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\uff0c\u8fd9\u4e2a\u65b0\u51fd\u6570\u53ef\u4ee5\u56fa\u5b9a\u4f4f\u539f\u51fd\u6570\u7684\u90e8\u5206\u53c2\u6570\uff0c\u4ece\u800c\u5728\u8c03\u7528\u65f6\u66f4\u7b80\u5355\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u504f\u51fd\u6570\uff08Partial function\uff09\u662fPython\u7684functools\u6a21\u5757\u63d0\u4f9b\u7684\u4e00\u4e2a\u5f88\u6709\u7528\u7684\u529f\u80fd\u3002 \u7b80\u5355\u603b\u7ed3functools.p   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[35],"tags":[16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8817"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8817"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8817\/revisions"}],"predecessor-version":[{"id":8819,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/8817\/revisions\/8819"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8817"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}