{"id":3210,"date":"2025-06-03T15:36:28","date_gmt":"2025-06-04T08:35:27","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3210"},"modified":"2025-06-04T15:36:28","modified_gmt":"2025-06-04T08:35:27","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84functools%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84functools%e5%87%bd%e6%95%b8\/","title":{"rendered":"2025 \u6700\u65b0\u7248\uff1a\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 functools \u51fd\u6578\u6a21\u7d44"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, functools, Python \u6559\u5b78, \u51fd\u6578\u6a21\u7d44, \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<h1>Python \u4e2d\u7684 functools \u6a21\u7d44<\/h1>\n<p>Python \u4e2d\u7684 functools \u6a21\u7d44\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5f37\u5927\u7684\u9ad8\u968e\u51fd\u5f0f\uff0c\u9019\u4e9b\u51fd\u5f0f\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u7387\u5730\u64cd\u4f5c\u51fd\u5f0f\uff0c\u8b93\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u8207\u53ef\u8b80\u3002\u96a8\u8457\u7a0b\u5f0f\u958b\u767c\u7684\u9700\u6c42\u4e0d\u65b7\u8b8a\u5316\uff0c\u4e86\u89e3\u9019\u4e9b\u51fd\u5f0f\u7684\u6700\u65b0\u7528\u6cd5\u5c0d\u65bc\u6bcf\u4f4d Python \u958b\u767c\u8005\u4f86\u8aaa\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002<\/p>\n<h2>functools \u6a21\u7d44\u7684\u4e3b\u8981\u529f\u80fd<\/h2>\n<p>functools \u6a21\u7d44\u5305\u542b\u591a\u500b\u9ad8\u968e\u51fd\u5f0f\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u5e38\u7528\u7684\u51fd\u5f0f\u53ca\u5176\u529f\u80fd\uff1a<\/p>\n<ul>\n<li><strong>partial()<\/strong>\uff1a\u5c07\u51fd\u5f0f\u7684\u67d0\u4e9b\u53c3\u6578\u56fa\u5b9a\uff0c\u4e26\u56de\u50b3\u4e00\u500b\u65b0\u7684\u51fd\u5f0f\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u547c\u53eb\u51fd\u5f0f\u6642\u9700\u8981\u50b3\u5165\u7684\u53c3\u6578\u6578\u91cf\u3002<\/li>\n<li><strong>reduce()<\/strong>\uff1a\u5c0d\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u4f7f\u7528\u6307\u5b9a\u7684\u51fd\u5f0f\uff0c\u4e26\u5c07\u5176\u7d50\u679c\u7d2f\u7a4d\u3002\u9019\u5728\u8655\u7406\u5217\u8868\u6642\u7279\u5225\u6709\u7528\u3002<\/li>\n<li><strong>map()<\/strong>\uff1a\u5c07\u6307\u5b9a\u7684\u51fd\u5f0f\u4f5c\u7528\u65bc\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u500b\u5143\u7d20\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5e8f\u5217\uff0c\u5305\u542b\u6240\u6709\u51fd\u5f0f\u7684\u7d50\u679c\u3002<\/li>\n<li><strong>filter()<\/strong>\uff1a\u6839\u64da\u7d66\u5b9a\u7684\u51fd\u5f0f\u904e\u6ffe\u5e8f\u5217\uff0c\u50c5\u4fdd\u7559\u90a3\u4e9b\u4f7f\u51fd\u5f0f\u8fd4\u56de True \u7684\u5143\u7d20\u3002<\/li>\n<li><strong>cmp_to_key()<\/strong>\uff1a\u5c07\u6bd4\u8f03\u51fd\u5f0f\u8f49\u63db\u70ba key \u51fd\u5f0f\uff0c\u4ee5\u4fbf\u5728\u6392\u5e8f\u6642\u4f7f\u7528\u3002<\/li>\n<li><strong>update_wrapper()<\/strong>\uff1a\u5c07\u4e00\u500b\u51fd\u5f0f\u7684\u5c6c\u6027\u66f4\u65b0\u5230\u53e6\u4e00\u500b\u51fd\u5f0f\u4e2d\uff0c\u9019\u5c0d\u65bc\u88dd\u98fe\u5668\u7279\u5225\u6709\u7528\u3002<\/li>\n<li><strong>wraps()<\/strong>\uff1a\u7528\u65bc\u88dd\u98fe\u5668\uff0c\u5e6b\u52a9\u66f4\u65b0\u51fd\u5f0f\u7684\u540d\u7a31\u548c\u6587\u6a94\u5b57\u4e32\uff0c\u8b93\u5b83\u5011\u66f4\u5bb9\u6613\u7406\u89e3\u3002<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u51fd\u6578\u4e0d\u50c5\u8b93\u7a0b\u5f0f\u78bc\u66f4\u7c21\u6f54\uff0c\u540c\u6642\u4e5f\u63d0\u5347\u4e86\u57f7\u884c\u6548\u7387\uff0c\u662f Python \u7a0b\u5f0f\u958b\u767c\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<h2>\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a\u4f7f\u7528 functools \u6a21\u7d44<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 functools \u6a21\u7d44\u7684\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0c\u5c55\u793a\u5982\u4f55\u5229\u7528 <strong>partial()<\/strong> \u51fd\u5f0f\u4f86\u56fa\u5b9a\u53c3\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport functools<\/p>\n<p>def add(a, b):<br \/>\n    return a + b<\/p>\n<p># \u4f7f\u7528 partial \u51fd\u5f0f\u5c07\u7b2c\u4e00\u500b\u53c3\u6578\u56fa\u5b9a\u70ba 5<br \/>\nadd_five = functools.partial(add, 5)<\/p>\n<p># \u547c\u53eb\u65b0\u7684\u51fd\u5f0f\uff0c\u50c5\u9700\u50b3\u5165\u7b2c\u4e8c\u500b\u53c3\u6578<br \/>\nprint(add_five(3))  # \u8f38\u51fa: 8<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86 functools \u6a21\u7d44\u7684 <strong>partial()<\/strong> \u51fd\u5f0f\uff0c\u56fa\u5b9a\u4e86 <strong>add()<\/strong> \u51fd\u5f0f\u7684\u7b2c\u4e00\u500b\u53c3\u6578\u70ba 5\uff0c\u4e26\u8fd4\u56de\u4e86\u4e00\u500b\u65b0\u7684\u51fd\u5f0f <strong>add_five()<\/strong>\u3002\u7576\u6211\u5011\u547c\u53eb <strong>add_five()<\/strong> \u6642\uff0c\u53ea\u9700\u50b3\u5165\u4e00\u500b\u53c3\u6578\uff0c\u4fbf\u53ef\u5b8c\u6210\u52a0\u6cd5\u904b\u7b97\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 functools \u6a21\u7d44\u6642\uff0c\u958b\u767c\u8005\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **TypeError**\uff1a\u7576\u4f7f\u7528 <strong>partial()<\/strong> \u6642\uff0c\u5982\u679c\u63d0\u4f9b\u7684\u53c3\u6578\u6578\u91cf\u4e0d\u6b63\u78ba\uff0c\u5c07\u5c0e\u81f4\u932f\u8aa4\u3002\u78ba\u4fdd\u6aa2\u67e5\u51fd\u5f0f\u7684\u53c3\u6578\u6578\u91cf\u8207\u985e\u578b\u3002<br \/>\n&#8211; **AttributeError**\uff1a\u4f7f\u7528 <strong>wraps()<\/strong> \u6642\uff0c\u5fd8\u8a18\u5c07\u88dd\u98fe\u5668\u8fd4\u56de\u7684\u51fd\u5f0f\u9032\u884c\u9069\u7576\u7684\u5305\u88dd\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u5c6c\u6027\u4e1f\u5931\u3002<\/p>\n<p>\u70ba\u4e86\u907f\u514d\u9019\u4e9b\u554f\u984c\uff0c\u5efa\u8b70\u5728\u4f7f\u7528\u9019\u4e9b\u51fd\u5f0f\u6642\u4ed4\u7d30\u95b1\u8b80\u5b98\u65b9\u6587\u6a94\uff0c\u4e26\u9032\u884c\u5145\u5206\u7684\u6e2c\u8a66\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u51fd\u5f0f\u529f\u80fd\u5f37\u5927\u4e14\u9748\u6d3b\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u5229\u7528 functools \u6a21\u7d44\u4f86\u5be6\u73fe\u4ee5\u4e0b\u61c9\u7528\uff1a<\/p>\n<p>&#8211; **\u8cc7\u6599\u8655\u7406**\uff1a\u4f7f\u7528 <strong>map()<\/strong> \u548c <strong>filter()<\/strong> \u4f86\u8655\u7406\u548c\u6e05\u7406\u8cc7\u6599\u96c6\u3002<br \/>\n&#8211; **\u6027\u80fd\u512a\u5316**\uff1a\u5229\u7528 <strong>reduce()<\/strong> \u63d0\u9ad8\u5927\u578b\u6578\u64da\u96c6\u7684\u904b\u7b97\u6548\u7387\u3002<br \/>\n&#8211; **\u88dd\u98fe\u5668**\uff1a\u4f7f\u7528 <strong>wraps()<\/strong> \u4f86\u8a2d\u8a08\u81ea\u5b9a\u7fa9\u88dd\u98fe\u5668\uff0c\u589e\u5f37\u51fd\u5f0f\u7684\u529f\u80fd\u3002<\/p>\n<p>\u82e5\u60f3\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u9032\u968e\u6982\u5ff5\uff0c\u5efa\u8b70\u53c3\u8003 [Vocus \u6559\u5b78](https:\/\/vocus.cc\/) \u6216 [Miner \u6559\u5b78](https:\/\/miner.tw\/) \u76f8\u95dc\u5167\u5bb9\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Python \u4e2d\u7684 functools \u6a21\u7d44\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u9ad8\u6548\u5730\u64cd\u4f5c\u51fd\u5f0f\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\u3002\u900f\u904e\u638c\u63e1\u9019\u4e9b\u9ad8\u968e\u51fd\u5f0f\uff0c\u60a8\u5c07\u80fd\u5920\u7de8\u5beb\u51fa\u66f4\u7c21\u6f54\u3001\u9ad8\u6548\u7684\u4ee3\u78bc\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: functools \u6a21\u7d44\u4e2d\u7684 partial() \u51fd\u5f0f\u6709\u54ea\u4e9b\u61c9\u7528\uff1f**<br \/>\nA1: partial() \u51fd\u5f0f\u53ef\u7528\u65bc\u7c21\u5316\u51fd\u5f0f\u547c\u53eb\uff0c\u7279\u5225\u662f\u5728\u9700\u8981\u591a\u6b21\u547c\u53eb\u76f8\u540c\u51fd\u5f0f\u4f46\u53ea\u6539\u8b8a\u90e8\u5206\u53c3\u6578\u6642\uff0c\u80fd\u63d0\u4f9b\u6975\u5927\u7684\u4fbf\u5229\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u4f7f\u7528 functools \u7684 reduce() \u51fd\u5f0f\uff1f**<br \/>\nA2: reduce() \u51fd\u5f0f\u901a\u5e38\u7528\u65bc\u7d2f\u7a4d\u904b\u7b97\uff0c\u5982\u8a08\u7b97\u5217\u8868\u4e2d\u6240\u6709\u6578\u5b57\u7684\u7e3d\u548c\u6216\u4e58\u7a4d\u3002\u4f7f\u7528\u6642\u9700\u63d0\u4f9b\u4e00\u500b\u51fd\u5f0f\u548c\u4e00\u500b\u5e8f\u5217\uff0c\u51fd\u5f0f\u5c07\u4f9d\u5e8f\u8655\u7406\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<p>**Q3: functools \u7684 wraps() \u70ba\u4f55\u91cd\u8981\uff1f**<br \/>\nA3: wraps() \u80fd\u5920\u4fdd\u7559\u88ab\u88dd\u98fe\u51fd\u5f0f\u7684\u539f\u59cb\u540d\u7a31\u548c\u6587\u6a94\u5b57\u4e32\uff0c\u9019\u5c0d\u65bc\u7dad\u8b77\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u81f3\u95dc\u91cd\u8981\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684functools\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u60a8\u66f4\u6709\u6548\u5730\u7ba1\u7406\u51fd\u6578\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u4ecb\u7d39functools\u51fd\u6578\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\uff0c\u4ee5\u4fbf\u60a8\u53ef\u4ee5\u66f4\u597d\u5730\u5229\u7528\u5b83\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3210","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-PM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3210"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3210\/revisions"}],"predecessor-version":[{"id":3211,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3210\/revisions\/3211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}