{"id":3238,"date":"2025-06-03T16:27:12","date_gmt":"2025-06-04T08:30:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3238"},"modified":"2025-06-04T16:27:12","modified_gmt":"2025-06-04T08:30:12","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84namedtuple%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%84namedtuple%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 namedtuple() \u51fd\u6578\uff1a2025 \u6700\u65b0\u5be6\u4f5c\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, namedtuple(), Python \u6559\u5b78, Python \u6578\u64da\u7d50\u69cb\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 namedtuple()<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`namedtuple()` \u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5efa\u7acb\u4e00\u500b\u5177\u6709\u547d\u540d\u6b04\u4f4d\u7684 tuple\uff0c\u5f9e\u800c\u66f4\u65b9\u4fbf\u5730\u8a2a\u554f\u548c\u7ba1\u7406\u6578\u64da\u3002\u9019\u4f7f\u5f97\u4f60\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u6e05\u6670\u4e14\u6613\u65bc\u7dad\u8b77\u3002\u672c\u6587\u5c07\u4ecb\u7d39 `namedtuple()` \u7684\u57fa\u672c\u7528\u6cd5\u3001\u61c9\u7528\u5834\u666f\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u8b93\u4f60\u80fd\u5920\u5728 2025 \u5e74\u7684 Python \u958b\u767c\u4e2d\u5145\u5206\u5229\u7528\u9019\u500b\u529f\u80fd\u3002<\/p>\n<h2>namedtuple() \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u5728\u4f7f\u7528 `namedtuple()` \u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u5b9a\u7fa9\u4e00\u500b tuple \u7684\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `namedtuple()` \u4f86\u5b9a\u7fa9\u4e00\u500b\u540d\u70ba `Point` \u7684\u6a21\u578b\uff0c\u5305\u542b `x` \u548c `y` \u5169\u500b\u5143\u7d20\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom collections import namedtuple<\/p>\n<p>Point = namedtuple(&#8216;Point&#8217;, [&#8216;x&#8217;, &#8216;y&#8217;])<br \/>\n&#8220;`<\/p>\n<p>\u5b9a\u7fa9\u597d\u6a21\u578b\u5f8c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u9019\u500b `Point` \u4f86\u5efa\u7acb\u65b0\u7684 tuple \u5be6\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\np = Point(11, y=22)<br \/>\n&#8220;`<\/p>\n<p>\u6b64\u6642\uff0c\u4f60\u5df2\u7d93\u5275\u5efa\u4e86\u4e00\u500b\u65b0\u7684 `Point` tuple\uff0c`x` \u7684\u503c\u70ba 11\uff0c`y` \u7684\u503c\u70ba 22\u3002\u4f60\u53ef\u4ee5\u901a\u904e\u547d\u540d\u4f86\u8a2a\u554f\u9019\u4e9b\u5143\u7d20\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(p.x + p.y)  # 33<br \/>\n&#8220;`<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u5c07 `namedtuple` \u8f49\u63db\u70ba\u666e\u901a\u7684 tuple\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(tuple(p))  # (11, 22)<br \/>\n&#8220;`<\/p>\n<h2>namedtuple() \u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>`namedtuple()` \u5728\u8a31\u591a\u5834\u666f\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u61c9\u7528\uff1a<\/p>\n<ul>\n<li>**\u8cc7\u6599\u5eab\u8655\u7406**\uff1a\u7576\u4f60\u5f9e\u8cc7\u6599\u5eab\u4e2d\u7372\u53d6\u6578\u64da\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 `namedtuple()` \u4f86\u5c07\u7d50\u679c\u8f49\u63db\u6210\u5177\u540d\u7684 tuple\uff0c\u9019\u6a23\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8a2a\u554f\u5404\u500b\u5b57\u6bb5\u3002<\/li>\n<li>**JSON \u6578\u64da\u8655\u7406**\uff1a\u5728\u8655\u7406 JSON \u8cc7\u6599\u6642\uff0c`namedtuple()` \u53ef\u4ee5\u5e6b\u52a9\u4f60\u5c07\u5176\u8f49\u63db\u70ba\u5177\u540d\u7684 tuple\uff0c\u8b93\u6578\u64da\u8a2a\u554f\u8b8a\u5f97\u66f4\u76f4\u89c0\u3002<\/li>\n<li>**API \u8acb\u6c42\u8655\u7406**\uff1a\u7576\u8655\u7406\u7db2\u8def\u8acb\u6c42\u7684\u8cc7\u6599\u6642\uff0c\u4f7f\u7528 `namedtuple()` \u53ef\u4ee5\u7c21\u5316\u6578\u64da\u7684\u7ba1\u7406\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<\/ul>\n<h2>\u6700\u4f73\u5be6\u8e10\u8207\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u5be6\u4f5c `namedtuple()` \u6642\uff0c\u6709\u5e7e\u9ede\u6700\u4f73\u5be6\u8e10\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n<p>1. **\u4f7f\u7528\u7c21\u6f54\u7684\u547d\u540d**\uff1a\u78ba\u4fdd\u4f60\u7684\u547d\u540d\u6e05\u6670\u4e14\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u9019\u6a23\u80fd\u8b93\u5176\u4ed6\u958b\u767c\u8005\u5feb\u901f\u7406\u89e3\u4f60\u7684\u4ee3\u78bc\u3002<br \/>\n2. **\u907f\u514d\u904e\u5ea6\u5d4c\u5957**\uff1a\u96d6\u7136\u4f60\u53ef\u4ee5\u5d4c\u5957 `namedtuple`\uff0c\u4f46\u904e\u5ea6\u5d4c\u5957\u6703\u8b93\u4ee3\u78bc\u8b8a\u5f97\u8907\u96dc\uff0c\u5efa\u8b70\u4f7f\u7528\u5176\u4ed6\u6578\u64da\u7d50\u69cb\u5982 `dataclass` \u4f86\u8655\u7406\u66f4\u8907\u96dc\u7684\u6578\u64da\u6a21\u578b\u3002<br \/>\n3. **\u5be6\u65bd\u932f\u8aa4\u8655\u7406**\uff1a\u7576\u4f60\u4f7f\u7528 `namedtuple()` \u6642\uff0c\u78ba\u4fdd\u5c0d\u8f38\u5165\u6578\u64da\u9032\u884c\u9a57\u8b49\uff0c\u4ee5\u907f\u514d\u56e0\u6578\u64da\u683c\u5f0f\u4e0d\u6b63\u78ba\u800c\u5c0e\u81f4\u7684\u932f\u8aa4\u3002<\/p>\n<p>\u82e5\u5728\u4f7f\u7528\u904e\u7a0b\u4e2d\u9047\u5230\u932f\u8aa4\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<p>&#8211; \u78ba\u4fdd\u4f60\u6b63\u78ba\u5c0e\u5165\u4e86 `collections.namedtuple`\u3002<br \/>\n&#8211; \u78ba\u4fdd\u4f60\u9075\u5faa\u6b63\u78ba\u7684\u8a9e\u6cd5\u4f86\u5efa\u7acb\u548c\u4f7f\u7528 `namedtuple`\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528\u8207\u8cc7\u6e90<\/h2>\n<p>\u60f3\u8981\u6df1\u5165\u4e86\u89e3 Python \u7684\u6578\u64da\u7d50\u69cb\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u5efa\u8b70\u53c3\u8003\u4ee5\u4e0b\u8cc7\u6e90\uff1a[Python Data Structures \u6559\u5b78](https:\/\/vocus.cc\/article\/605c10d1fd89780001c0e1b7)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86 Python \u4e2d\u7684 `namedtuple()` \u51fd\u5f0f\uff0c\u9019\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u4f60\u5efa\u7acb\u5177\u540d\u7684 tuple\uff0c\u7c21\u5316\u6578\u64da\u8a2a\u554f\u3002\u901a\u904e\u5be6\u4f5c\u7bc4\u4f8b\u3001\u61c9\u7528\u5834\u666f\u548c\u6700\u4f73\u5be6\u8e10\u7684\u4ecb\u7d39\uff0c\u5e0c\u671b\u4f60\u80fd\u5728\u672a\u4f86\u7684 Python \u958b\u767c\u4e2d\u5145\u5206\u5229\u7528 `namedtuple()` \u7684\u512a\u52e2\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: namedtuple() \u548c\u666e\u901a tuple \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `namedtuple()` \u5141\u8a31\u4f60\u4f7f\u7528\u540d\u7a31\u4f86\u8a2a\u554f\u5143\u7d20\uff0c\u800c\u666e\u901a\u7684 tuple \u53ea\u80fd\u901a\u904e\u7d22\u5f15\u8a2a\u554f\u3002\u9019\u4f7f\u5f97\u4ee3\u78bc\u66f4\u52a0\u53ef\u8b80\u3002<\/p>\n<p>**Q2: namedtuple() \u6703\u4f54\u7528\u66f4\u591a\u7684\u5167\u5b58\u55ce\uff1f**<br \/>\nA2: \u76f8\u8f03\u65bc\u666e\u901a\u7684 tuple\uff0c`namedtuple()` \u6703\u7565\u4f54\u7528\u66f4\u591a\u5167\u5b58\uff0c\u56e0\u70ba\u5b83\u5b58\u5132\u4e86\u5b57\u6bb5\u540d\u7a31\u3002\u4f46\u9019\u901a\u5e38\u4e0d\u662f\u554f\u984c\uff0c\u56e0\u70ba\u5b83\u5e36\u4f86\u7684\u53ef\u8b80\u6027\u63d0\u5347\u662f\u503c\u5f97\u7684\u3002<\/p>\n<p>**Q3: \u6709\u6c92\u6709\u5176\u4ed6\u985e\u4f3c\u7684\u6578\u64da\u7d50\u69cb\uff1f**<br \/>\nA3: \u662f\u7684\uff0cPython \u9084\u63d0\u4f9b\u4e86 `dataclass`\uff0c\u9019\u662f\u4e00\u500b\u66f4\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u65bc\u5275\u5efa\u53ef\u8b8a\u7684\u6578\u64da\u985e\u578b\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684namedtuple()\u51fd\u6578\uff0c\u5b83\u662f\u4e00\u7a2e\u53ef\u4ee5\u8b93\u4f60\u5efa\u7acb\u81ea\u5b9a\u7fa9\u7684tuple\u7269\u4ef6\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u7ba1\u7406\u8cc7\u6599\uff0c\u4e26\u4e14\u66f4\u5bb9\u6613\u95b1\u8b80\u3002\u672c\u6587\u5c07\u4ecb\u7d39namedtuple()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\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-3238","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-Qe","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3238","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=3238"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3238\/revisions"}],"predecessor-version":[{"id":12893,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3238\/revisions\/12893"}],"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=3238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}