{"id":191148,"date":"2020-05-26T09:11:24","date_gmt":"2020-05-26T01:11:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=191148"},"modified":"2020-05-17T16:12:38","modified_gmt":"2020-05-17T08:12:38","slug":"introduction-and-application","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/introduction-and-application.html","title":{"rendered":"Python \u504f\u51fd\u6570\u4ecb\u7ecd\u53ca\u5e94\u7528"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPython\u7684functools\u6a21\u5757\u63d0\u4f9b\u4e86\u5f88\u591a\u6709\u7528\u7684\u529f\u80fd,\u5176\u4e2d\u4e00\u4e2a\u5c31\u662f\u504f\u51fd\u6570(Partial function)\u3002\u8981\u6ce8\u610f,\u8fd9\u91cc\u7684\u504f\u51fd\u6570\u548c\u6570\u5b66\u610f\u4e49\u4e0a\u7684\u504f\u51fd\u6570\u4e0d\u4e00\u6837\u3002Python \u504f\u51fd\u6570\u662f\u901a\u8fc7 functools \u6a21\u5757\u88ab\u7528\u6237\u8c03\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u504f\u51fd\u6570 partial \u5e94\u7528<\/strong><\/div>\n

\u51fd\u6570\u5728\u6267\u884c\u65f6\uff0c\u8981\u5e26\u4e0a\u6240\u6709\u5fc5\u8981\u7684\u53c2\u6570\u8fdb\u884c\u8c03\u7528\u3002\u4f46\u662f\uff0c\u6709\u65f6\u53c2\u6570\u53ef\u4ee5\u5728\u51fd\u6570\u88ab\u8c03\u7528\u4e4b\u524d\u63d0\u524d\u83b7\u77e5\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u51fd\u6570\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\u9884\u5148\u5c31\u80fd\u7528\u4e0a\uff0c\u4ee5\u4fbf\u51fd\u6570\u80fd\u7528\u66f4\u5c11\u7684\u53c2\u6570\u8fdb\u884c\u8c03\u7528\u3002<\/p>\n

\u504f\u51fd\u6570\u662f\u5c06\u6240\u8981\u627f\u8f7d\u7684\u51fd\u6570\u4f5c\u4e3apartial()\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c\u539f\u51fd\u6570\u7684\u5404\u4e2a\u53c2\u6570\u4f9d\u6b21\u4f5c\u4e3apartial()\u51fd\u6570\u540e\u7eed\u7684\u53c2\u6570\uff0c\u9664\u975e\u4f7f\u7528\u5173\u952e\u5b57\u53c2\u6570\u3002<\/p>\n

\u901a\u8fc7\u8bed\u8a00\u63cf\u8ff0\u53ef\u80fd\u65e0\u6cd5\u7406\u89e3\u504f\u51fd\u6570\u662f\u600e\u4e48\u4f7f\u7528\u7684\uff0c\u90a3\u4e48\u5c31\u4e3e\u4e00\u4e2a\u5e38\u89c1\u7684\u4f8b\u5b50\u6765\u8bf4\u660e\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\uff0c\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u53d6\u4f59\u51fd\u6570\uff0c\u5bf9\u4e8e\u6574\u6570 100\uff0c\u53d6\u5f97\u5bf9\u4e8e\u4e0d\u540c\u6570 m \u7684 100%m \u7684\u4f59\u6570\u3002<\/p>\n

from functools import partial\r\n \r\ndef mod( n, m ):\r\n  return n % m\r\n \r\nmod_by_100 = partial( mod, 100 )\r\n \r\nprint mod( 100, 7 )  # 2\r\nprint mod_by_100( 7 )  # 2<\/pre>\n

\u7531\u4e8e\u4e4b\u524d\u770b\u5230\u7684\u4f8b\u5b50\u4e00\u822c\u9009\u62e9\u52a0\u6cd5\u6216\u4e58\u6cd5\u6765\u8bb2\u89e3\uff0c\u65e0\u6cd5\u4f53\u4f1a\u504f\u51fd\u6570\u53c2\u6570\u7684\u4f4d\u7f6e\u95ee\u9898\uff0c\u5bb9\u6613\u7ed9\u4eba\u9020\u6210 partial \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e5f\u662f\u539f\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u5047\u8c61\uff0c\u6240\u4ee5\u6211\u5728\u8fd9\u91cc\u9009\u62e9 mod \u6765\u8bb2\u89e3\u3002<\/p>\n

\u800c\u5bf9\u4e8e\u6709\u5173\u952e\u5b57\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u5c31\u53ef\u4ee5\u4e0d\u6309\u7167\u539f\u51fd\u6570\u7684\u53c2\u6570\u4f4d\u7f6e\u548c\u4e2a\u6570\u4e86\u3002\u4e0b\u9762\u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u8bb2\u7684\u662f\u5982\u4f55\u8fdb\u884c\u4e0d\u540c\u7684\u8fdb\u5236\u8f6c\u6362\u3002<\/p>\n

from functools import partial\r\n \r\nbin2dec = partial( int, base=2 )\r\nprint bin2dec( '0b10001' )  # 17\r\nprint bin2dec( '10001' )  # 17\r\n \r\nhex2dec = partial( int, base=16 )\r\nprint hex2dec( '0x67' )  # 103\r\nprint hex2dec( '67' )  # 103<\/pre>\n

\u504f\u51fd\u6570\u7684\u8fd9\u4e9b\u5e94\u7528\u770b\u4f3c\u7b80\u5355\uff0c\u7528\u9014\u5374\u5f88\u5927\uff0c\u53ef\u4ee5\u5f88\u597d\u7684\u6267\u884cDRY\u539f\u5219\uff0c\u8282\u7701\u7f16\u7a0b\u6210\u672c\u3002<\/p>\n

\u5173\u4e8e\u504f\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u73b0\u6709\u51fd\u6570\u8fdb\u884c\u52a0\u5f3a\u3002<\/p>\n

>>> import functools\r\n>>> int2 =functools.partial (int, base=2) # \u628a int \u7684\u8f6c\u6362\u8bbe\u4e3a\u4e8c\u8fdb\u5236\u4e86\uff0c\u8fd9\u91cc base \u662f int \u51fd\u6570\u8868\u793a\u8fdb\u5236\u7684\u53c2\u6570\u3002\r\n>>>int2('1000000')\r\n64\r\n\r\n>>>int2('1010101')\r\n85<\/pre>\n

\u5728\u91cd\u8bbe\u4e4b\u540e, \u4e5f\u53ef\u4ee5\u5728\u51fd\u6570\u8c03\u7528\u65f6\u4f20\u5165\u5176\u4ed6\u503c:<\/p>\n

\u200b>>> int2('1000000', base=10)  # \u8fd9\u91cc base \u53d8\u6210\u4e86 10\uff0c\u8986\u76d6\u4e86\u5df2\u8bbe\u7684\u9ed8\u8ba4\u503c 2\u3002\r\n1000000<\/pre>\n

\u6ce8\u610f\u8fd9\u91cc\u5728\u521b\u5efa\u65b0\u7684\u504f\u51fd\u6570\u540e\uff0c\u4f9d\u65e7\u53ef\u4ee5\u66f4\u6539\u5df2\u7ecf\u8bbe\u7f6e\u7684\u9ed8\u8ba4\u503c\uff0c\u4f46\u662f\u5fc5\u987b\u6e05\u6670\u6307\u51fa\u662f\u66f4\u6539\u4e86 base \u7684\u503c\u3002<\/p>\n

\u5426\u5219\u5982\u679c\u76f4\u63a5\u4f20\u5982\u4e00\u4e2a\u6570\u503c\u4f1a\u62a5\u9519:<\/p>\n

int2('100', 10)  # \u62a5\u9519\uff0c10 \u524d\u672a\u52a0 base=\uff0c\u4e0d\u80fd\u5206\u8fa8\u8fd9\u662f\u4f20\u7ed9 base \u7684<\/pre>\n

\u539f\u56e0\u5982\u4e0b:<\/p>\n

\u521b\u5efa\u504f\u51fd\u6570\u65f6\uff0c\u5b9e\u9645\u4e0a\u53ef\u4ee5\u63a5\u6536\u51fd\u6570\u5bf9\u8c61\u3001*args \u548c **kw \u8fd9 3 \u4e2a\u53c2\u6570\uff0c\u5f53\u4f20\u5165\uff1a<\/p>\n

int2 =functools.partial(int, base=2)  # int \u662f\u51fd\u6570\u5bf9\u8c61\uff0cbase=2 \u662f **kw\uff0c\u6ca1\u6709\u4f20\u5165 *args \u53c2\u6570<\/pre>\n

\u5b9e\u9645\u4e0a\u56fa\u5b9a\u4e86 int() \u51fd\u6570\u7684\u5173\u952e\u5b57\u53c2\u6570 base\uff0c\u4e5f\u5c31\u662f\uff1a<\/p>\n

int2('10010')<\/pre>\n

\u76f8\u5f53\u4e8e\uff1a<\/p>\n

kw = { 'base': 2 }\r\nint('10010', **kw)  # \u5982\u679c\u4e0d\u6307\u660e base=2 \u800c\u53ea\u662f\u4f20\u5165 2 \u7684\u8bdd\uff0c\u90a3\u4e48 2 \u88ab\u8ba4\u4e3a\u662f *args \u7684\u503c<\/pre>\n

\u5f53\u4f20\u5165\uff1a<\/p>\n

max2 =functools.partial(max, 10)  # \u8fd9\u91cc\u7684 10 \u663e\u7136\u5c31\u662f\u4f5c\u4e3a *args \u91cc\u7684\u503c\u4f20\u5165\u7684<\/pre>\n

\u5b9e\u9645\u4e0a\u4f1a\u628a 10 \u4f5c\u4e3a *args \u7684\u4e00\u90e8\u5206\u81ea\u52a8\u52a0\u5230\u53c2\u6570\u5217\u8868\u91cc\u53bb\uff0c\u4e5f\u5c31\u662f\uff1a<\/p>\n

max2(5,6, 7)  # \u539f\u672c\u6709 *args \u7684\u5217\u8868\uff0c\u7136\u540e\u4f1a\u628a 10 \u518d\u52a0\u8fdb\u53bb<\/pre>\n

\u76f8\u5f53\u4e8e\uff1a<\/p>\n

args = (10, 5, 6, 7)\r\nmax(*args)<\/pre>\n

\u7ed3\u679c\u4e3a 10\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u51fd\u6570\u5728\u6267\u884c\u65f6\uff0c\u8981\u5e26\u4e0a\u6240\u6709\u5fc5\u8981\u7684\u53c2\u6570\u8fdb\u884c\u8c03\u7528\u3002\u4f46\u662f\uff0c\u6709\u65f6\u53c2\u6570\u53ef\u4ee5\u5728\u51fd\u6570\u88ab\u8c03\u7528\u4e4b\u524d\u63d0\u524d\u83b7\u77e5\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e00\u4e2a\u51fd\u6570\u6709 […]<\/p>\n","protected":false},"author":1470,"featured_media":191151,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-191148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=191148"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191148\/revisions"}],"predecessor-version":[{"id":191155,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191148\/revisions\/191155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/191151"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=191148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=191148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=191148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}