{"id":3404,"date":"2025-06-02T14:57:43","date_gmt":"2025-06-04T07:56:42","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3404"},"modified":"2025-06-04T14:57:43","modified_gmt":"2025-06-04T07:56:42","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84class_getitem","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84class_getitem\/","title":{"rendered":"\u5168\u9762\u89e3\u6790 Python \u7684 `class_getitem()`\uff1a2025 \u6700\u4f73\u5be6\u8e10\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, class_getitem(), Python \u6559\u5b78, Python \u985e\u5225\"><\/p>\n<h1>\u5168\u9762\u89e3\u6790 Python \u7684 <code>class_getitem()<\/code><\/h1>\n<p>\u5728 Python \u4e2d\uff0c`class_getitem()` \u662f\u4e00\u500b\u5f37\u5927\u7684\u7279\u6b8a\u65b9\u6cd5\uff0c\u5141\u8a31\u4f60\u5728\u985e\u5225\u4e2d\u4f7f\u7528\u7d22\u5f15\u4f86\u76f4\u63a5\u7372\u53d6\u7279\u5b9a\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\u3002\u9019\u500b\u529f\u80fd\u4e0d\u50c5\u63d0\u5347\u4e86\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\uff0c\u9084\u4f7f\u5f97\u985e\u5225\u7684\u4f7f\u7528\u66f4\u52a0\u9748\u6d3b\u548c\u9ad8\u6548\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e `class_getitem()` \u7684\u7528\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u53ca\u5176\u932f\u8aa4\u6392\u9664\u6280\u5de7\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f <code>class_getitem()<\/code>\uff1f<\/h2>\n<p>`class_getitem()` \u662f Python \u4e2d\u7528\u4f86\u91cd\u8f09\u7d22\u5f15\u64cd\u4f5c\u7684\u7279\u6b8a\u65b9\u6cd5\u3002\u9019\u610f\u5473\u8457\uff0c\u4f60\u53ef\u4ee5\u5b9a\u7fa9\u4e00\u500b\u81ea\u8a02\u7684\u7d22\u5f15\u884c\u70ba\uff0c\u4f7f\u5f97\u4f7f\u7528\u8005\u80fd\u5920\u901a\u904e\u7d22\u5f15\u76f4\u63a5\u8a2a\u554f\u985e\u5225\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\uff0c\u800c\u7121\u9700\u4f7f\u7528\u5b57\u5178\u6216\u5176\u4ed6\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\u3002<\/p>\n<h2><code>class_getitem()<\/code> \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u4f86\u770b\u770b `class_getitem()` \u7684\u57fa\u672c\u7528\u6cd5\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba `MyClass` \u7684\u985e\u5225\uff0c\u88e1\u9762\u5305\u542b\u4e86\u4e00\u500b\u5c6c\u6027 `my_attr` \u548c\u4e00\u500b\u65b9\u6cd5 `my_method`\u3002\u6211\u5011\u53ef\u4ee5\u900f\u904e\u91cd\u8f09 `__getitem__` \u65b9\u6cd5\u4f86\u5be6\u73fe\u7d22\u5f15\u529f\u80fd\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __getitem__(self, index):<br \/>\n        if index == &#8216;my_attr&#8217;:<br \/>\n            return self.my_attr<br \/>\n        elif index == &#8216;my_method&#8217;:<br \/>\n            return self.my_method<br \/>\n        else:<br \/>\n            raise KeyError(f&#8221;&#8216;{index}&#8217; \u4e0d\u5728\u985e\u5225\u5c6c\u6027\u4e2d&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u73fe\u5728\u6211\u5011\u53ef\u4ee5\u9019\u6a23\u4f7f\u7528\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_class = MyClass()<br \/>\nmy_attr = my_class[&#8216;my_attr&#8217;]\nmy_method = my_class[&#8216;my_method&#8217;]\n&#8220;`<\/p>\n<h2><code>class_getitem()<\/code> \u7684\u61c9\u7528\u793a\u4f8b<\/h2>\n<p>`class_getitem()` \u53ef\u4ee5\u7528\u65bc\u8a31\u591a\u5834\u666f\uff0c\u7279\u5225\u662f\u7576\u9700\u8981\u53ef\u8a2a\u554f\u6027\u6642\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u64f4\u5c55\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u66f4\u9748\u6d3b\u5730\u4f7f\u7528\u7d22\u5f15\u4f86\u8a2a\u554f\u5c6c\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self):<br \/>\n        self.my_attr = &#8220;\u9019\u662f\u5c6c\u6027\u503c&#8221;<br \/>\n        self.my_method = lambda: &#8220;\u9019\u662f\u65b9\u6cd5\u8fd4\u56de\u503c&#8221;<\/p>\n<p>    def __getitem__(self, index):<br \/>\n        if isinstance(index, str):<br \/>\n            if index == &#8216;my_attr&#8217;:<br \/>\n                return self.my_attr<br \/>\n            elif index == &#8216;my_method&#8217;:<br \/>\n                return self.my_method()<br \/>\n            else:<br \/>\n                raise KeyError(f&#8221;&#8216;{index}&#8217; \u4e0d\u5728\u985e\u5225\u5c6c\u6027\u4e2d&#8221;)<br \/>\n        else:<br \/>\n            raise TypeError(&#8220;\u7d22\u5f15\u5fc5\u9808\u662f\u5b57\u4e32&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b57\u4e32\u4f86\u7372\u53d6\u5c6c\u6027\u548c\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_class = MyClass()<br \/>\nprint(my_class[&#8216;my_attr&#8217;])    # \u8f38\u51fa\uff1a\u9019\u662f\u5c6c\u6027\u503c<br \/>\nprint(my_class[&#8216;my_method&#8217;])   # \u8f38\u51fa\uff1a\u9019\u662f\u65b9\u6cd5\u8fd4\u56de\u503c<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `class_getitem()` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **KeyError**: \u7576\u7d22\u5f15\u4e0d\u5728\u985e\u5225\u5c6c\u6027\u4e2d\u6642\uff0c\u61c9\u63d0\u4f9b\u6e05\u6670\u7684\u932f\u8aa4\u4fe1\u606f\u3002<br \/>\n&#8211; **TypeError**: \u7576\u7d22\u5f15\u985e\u578b\u4e0d\u6b63\u78ba\u6642\uff0c\u61c9\u660e\u78ba\u544a\u77e5\u7528\u6236\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u8a2a\u554f\uff0c`class_getitem()` \u9084\u53ef\u4ee5\u7528\u4f86\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u50cf\u662f\u591a\u7dad\u6578\u64da\u5b58\u53d6\u6216\u689d\u4ef6\u8a2a\u554f\u3002\u9019\u4f7f\u5f97 Python \u7684\u985e\u5225\u8a2d\u8a08\u66f4\u52a0\u9748\u6d3b\uff0c\u9069\u5408\u9032\u884c\u5404\u7a2e\u61c9\u7528\u958b\u767c\u3002<\/p>\n<h2>\u7d50\u8a9e<\/h2>\n<p>`class_getitem()` \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728 Python \u4e2d\u4ee5\u66f4\u76f4\u89c0\u7684\u65b9\u5f0f\u4f86\u8a2d\u8a08\u985e\u5225\u3002\u900f\u904e\u9019\u7a2e\u65b9\u6cd5\uff0c\u7a0b\u5e8f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u90fd\u5f97\u5230\u4e86\u986f\u8457\u63d0\u5347\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u66f4\u591a\u529f\u80fd\uff0c\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7\u6559\u7a0b\uff1a[Python \u6df1\u5165\u6559\u5b78](https:\/\/vocus.cc\/article\/123456) (\u8acb\u5c07\u9023\u7d50\u66ff\u63db\u70ba\u5be6\u969b\u7684 URL)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `class_getitem()` \u53ef\u4ee5\u91cd\u8f09\u54ea\u4e9b\u64cd\u4f5c\uff1f**<br \/>\nA1: `class_getitem()` \u4e3b\u8981\u7528\u65bc\u91cd\u8f09\u7d22\u5f15\u64cd\u4f5c\uff0c\u8b93\u4f60\u80fd\u5920\u4f7f\u7528\u7d22\u5f15\u4f86\u8a2a\u554f\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 `KeyError`\uff1f**<br \/>\nA2: \u7576\u7d22\u5f15\u4e0d\u6b63\u78ba\u6642\uff0c\u53ef\u4ee5\u5728 `__getitem__` \u65b9\u6cd5\u4e2d\u5f15\u767c `KeyError`\uff0c\u4e26\u63d0\u4f9b\u8a73\u7d30\u7684\u932f\u8aa4\u4fe1\u606f\u3002<\/p>\n<p>**Q3: `class_getitem()` \u7684\u6700\u4f73\u5be6\u8e10\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA3: \u4f7f\u7528 `class_getitem()` \u6642\uff0c\u61c9\u78ba\u4fdd\u7d22\u5f15\u985e\u578b\u7684\u6aa2\u67e5\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u932f\u8aa4\uff0c\u4e26\u63d0\u4f9b\u6e05\u6670\u7684\u932f\u8aa4\u8a0a\u606f\u4ee5\u5354\u52a9\u9664\u932f\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684class_getitem()\uff0c\u5b83\u662f\u4e00\u500b\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8b93\u4f60\u4f7f\u7528\u7d22\u5f15\u4f86\u5b58\u53d6\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u5f48\u6027\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64cd\u4f5c\u7269\u4ef6\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-3404","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-SU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3404","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=3404"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3404\/revisions"}],"predecessor-version":[{"id":12840,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3404\/revisions\/12840"}],"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=3404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}