{"id":3342,"date":"2025-05-29T16:08:35","date_gmt":"2025-06-04T08:10:34","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3342"},"modified":"2025-06-04T16:08:35","modified_gmt":"2025-06-04T08:10:34","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84class_getitem%e5%8a%9f%e8%83%bd-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84class_getitem%e5%8a%9f%e8%83%bd-2\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `__class_getitem__()` \u529f\u80fd\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __class_getitem__\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `__class_getitem__()` \u529f\u80fd<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__class_getitem__()` \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u985e\u5225\u4e2d\u4ee5\u985e\u4f3c\u65bc\u5b57\u5178\u7684\u65b9\u5f0f\u4f86\u5b58\u53d6\u8cc7\u6599\u3002\u9019\u4f7f\u5f97\u6211\u5011\u80fd\u5920\u5728\u985e\u5225\u4e2d\u4f7f\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u8cc7\u6599\uff0c\u800c\u4e0d\u9700\u8981\u660e\u78ba\u5b9a\u7fa9\u51fd\u6578\u6216\u5c6c\u6027\u3002\u96a8\u8457 Python \u8a9e\u8a00\u7684\u6f14\u9032\uff0c\u9019\u500b\u529f\u80fd\u5728\u73fe\u4eca\u7684\u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u8b8a\u5f97\u66f4\u52a0\u91cd\u8981\u3002<\/p>\n<h2>\u6700\u65b0\u7684 `__class_getitem__()` \u7528\u6cd5<\/h2>\n<p>\u5728 Python 3.9 \u53ca\u4ee5\u5f8c\u7684\u7248\u672c\u4e2d\uff0c`__class_getitem__()` \u7528\u65bc\u5b9a\u7fa9\u985e\u5225\u5c0d\u8c61\u5982\u4f55\u901a\u904e\u7d22\u5f15\u53d6\u5f97\u8cc7\u6599\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `__class_getitem__()` \u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9\u53ca\u4f7f\u7528\u6b64\u529f\u80fd\u3002<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self):<br \/>\n        self.data = {<br \/>\n            &#8216;name&#8217;: &#8216;John&#8217;,<br \/>\n            &#8216;age&#8217;: 20<br \/>\n        }<\/p>\n<p>    def __getitem__(self, key):<br \/>\n        return self.data[key]\n<p># \u5275\u5efa\u985e\u5225\u5be6\u4f8b<br \/>\nmy_class = MyClass()<br \/>\nname = my_class[&#8216;name&#8217;]  # \u4f7f\u7528\u7d22\u5f15\u5b58\u53d6\u8cc7\u6599<br \/>\nage = my_class[&#8216;age&#8217;]\nprint(f&#8221;Name: {name}, Age: {age}&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `MyClass` \u7684\u985e\u5225\uff0c\u4e26\u5728\u5176\u4e2d\u5be6\u4f5c\u4e86 `__getitem__()` \u65b9\u6cd5\uff0c\u9019\u6a23\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u7684\u65b9\u5f0f\u4f86\u5b58\u53d6 `data` \u5b57\u5178\u4e2d\u7684\u8cc7\u6599\u3002<\/p>\n<h2>\u64f4\u5c55\u61c9\u7528\uff1a\u4f7f\u7528 `__class_getitem__()` \u65bc\u8907\u96dc\u8cc7\u6599\u7d50\u69cb<\/h2>\n<p>`__class_getitem__()` \u7684\u61c9\u7528\u4e0d\u50c5\u9650\u65bc\u5b57\u5178\uff0c\u9084\u53ef\u4ee5\u61c9\u7528\u65bc\u5176\u4ed6\u8cc7\u6599\u7d50\u69cb\uff0c\u4f8b\u5982\u5217\u8868\u3001\u5143\u7d44\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `__class_getitem__()` \u65bc\u5217\u8868\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass ListContainer:<br \/>\n    def __init__(self):<br \/>\n        self.items = [&#8216;apple&#8217;, &#8216;banana&#8217;, &#8216;cherry&#8217;]\n<p>    def __getitem__(self, index):<br \/>\n        return self.items[index]\n<p># \u4f7f\u7528 ListContainer \u985e\u5225<br \/>\ncontainer = ListContainer()<br \/>\nprint(container[0])  # \u8f38\u51fa: apple<br \/>\nprint(container[1])  # \u8f38\u51fa: banana<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c`ListContainer` \u985e\u5225\u4f7f\u7528 `__getitem__()` \u4f86\u5be6\u73fe\u7d22\u5f15\u5b58\u53d6\u5217\u8868\u5143\u7d20\u7684\u529f\u80fd\uff0c\u975e\u5e38\u9069\u5408\u9700\u8981\u5feb\u901f\u5b58\u53d6\u8cc7\u6599\u7684\u60c5\u6cc1\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `__class_getitem__()` \u548c `__getitem__()` \u6642\uff0c\u6709\u5e7e\u500b\u5e38\u898b\u7684\u932f\u8aa4\u9700\u8981\u6392\u9664\uff1a<\/p>\n<p>1. **KeyError**\uff1a\u7576\u4f7f\u7528\u4e0d\u5b58\u5728\u7684\u9375\u7d22\u5f15\u6642\uff0c\u6703\u5f15\u767c `KeyError`\uff0c\u9019\u6642\u9700\u8981\u78ba\u4fdd\u6240\u7528\u7684\u9375\u5b58\u5728\u65bc\u8cc7\u6599\u7d50\u69cb\u4e2d\u3002<br \/>\n2. **IndexError**\uff1a\u5728\u5217\u8868\u4e2d\uff0c\u82e5\u7d22\u5f15\u8d85\u51fa\u7bc4\u570d\uff0c\u5247\u6703\u5f15\u767c `IndexError`\uff0c\u78ba\u4fdd\u7d22\u5f15\u503c\u5728\u6709\u6548\u7bc4\u570d\u5167\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u4e2d\u7684 `__class_getitem__()` \u662f\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u8b93\u958b\u767c\u8005\u5728\u985e\u5225\u4e2d\u4f7f\u7528\u7d22\u5f15\u7684\u65b9\u5f0f\u4f86\u5b58\u53d6\u8cc7\u6599\uff0c\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u4e5f\u4f7f\u5f97\u6578\u64da\u5b58\u53d6\u8b8a\u5f97\u66f4\u52a0\u76f4\u89c0\u3002\u96a8\u8457 Python \u7248\u672c\u7684\u66f4\u65b0\uff0c`__class_getitem__()` \u7684\u61c9\u7528\u5834\u666f\u6301\u7e8c\u64f4\u5c55\uff0c\u5c0d\u65bc\u73fe\u4ee3\u5316\u7684\u7a0b\u5f0f\u8a2d\u8a08\u5177\u6709\u91cd\u8981\u610f\u7fa9\u3002\u82e5\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5efa\u8b70\u53c3\u8003 [vocus.cc](https:\/\/vocus.cc) \u548c [miner.tw](https:\/\/miner.tw) \u7684\u76f8\u95dc\u6559\u5b78\u6587\u7ae0\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `__class_getitem__()` \u8207 `__getitem__()` \u6709\u4f55\u4e0d\u540c\uff1f**<br \/>\nA1: `__class_getitem__()` \u662f\u7528\u65bc\u985e\u5225\u5c64\u7d1a\u7684\u7d22\u5f15\u5b58\u53d6\uff0c\u800c `__getitem__()` \u5247\u662f\u5be6\u4f8b\u5c64\u7d1a\u7684\u7d22\u5f15\u5b58\u53d6\u3002\u524d\u8005\u7528\u65bc\u5b9a\u7fa9\u985e\u5225\u5c0d\u8c61\u5982\u4f55\u8655\u7406\u7d22\u5f15\uff0c\u5f8c\u8005\u5247\u662f\u5be6\u4f8b\u5982\u4f55\u8655\u7406\u7d22\u5f15\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 `KeyError` \u932f\u8aa4\uff1f**<br \/>\nA2: \u7576\u4f7f\u7528\u7d22\u5f15\u5b58\u53d6\u8cc7\u6599\u6642\uff0c\u61c9\u8a72\u5148\u6aa2\u67e5\u9375\u662f\u5426\u5b58\u5728\u65bc\u5c0d\u61c9\u7684\u8cc7\u6599\u7d50\u69cb\u4e2d\uff0c\u6216\u8005\u4f7f\u7528 `try-except` \u8a9e\u53e5\u4f86\u6355\u6349\u4e26\u8655\u7406 `KeyError`\u3002<\/p>\n<p>**Q3: `__class_getitem__()` \u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u8cc7\u6599\u7d50\u69cb\uff1f**<br \/>\nA3: \u9664\u4e86\u5b57\u5178\u548c\u5217\u8868\uff0c`__class_getitem__()` \u4e5f\u53ef\u4ee5\u7528\u65bc\u5143\u7d44\u53ca\u5176\u4ed6\u81ea\u5b9a\u7fa9\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u53ea\u8981\u6b63\u78ba\u5be6\u4f5c `__class_getitem__()` \u65b9\u6cd5\u5373\u53ef\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684class_getitem()\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u4f7f\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u63a7\u5236\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u4f7f\u7528Python\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-3342","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-RU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3342","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=3342"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3342\/revisions"}],"predecessor-version":[{"id":12863,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3342\/revisions\/12863"}],"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=3342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}