{"id":3448,"date":"2025-06-03T14:54:12","date_gmt":"2025-06-04T07:43:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3448"},"modified":"2025-06-04T14:54:12","modified_gmt":"2025-06-04T07:43:12","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84getitem%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84getitem%e5%8a%9f%e8%83%bd\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `__getitem__()` \u65b9\u6cd5\uff1a\u4f7f\u7528\u6280\u5de7\u8207\u5be6\u4f5c\u7bc4\u4f8b"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __getitem__()\"><\/p>\n<h1>Python \u4e2d\u7684 __getitem__() \u65b9\u6cd5<\/h1>\n<p>Python \u4e2d\u7684 `__getitem__()` \u65b9\u6cd5\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u51fd\u5f0f\uff0c\u5b83\u5141\u8a31\u4f60\u5f9e\u53ef\u8fed\u4ee3\u7684\u7269\u4ef6\u4e2d\u7372\u53d6\u6307\u5b9a\u5143\u7d20\u3002\u9019\u500b\u65b9\u6cd5\u9069\u7528\u65bc\u5b57\u5178\u3001\u5217\u8868\u3001\u5143\u7d44\u7b49\u591a\u7a2e\u53ef\u8fed\u4ee3\u7269\u4ef6\uff0c\u80fd\u5920\u6709\u6548\u5730\u63d0\u5347\u8cc7\u6599\u5b58\u53d6\u7684\u6548\u7387\u8207\u4fbf\u6377\u6027\u3002<\/p>\n<h2>__getitem__() \u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`__getitem__()` \u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u662f\u5c07\u5e0c\u671b\u53d6\u5f97\u7684\u5143\u7d20\u7684\u7d22\u5f15\u503c\u6216\u9375\u503c\u4f5c\u70ba\u53c3\u6578\u50b3\u5165\uff0c\u9019\u6a23\u4fbf\u80fd\u5feb\u901f\u53d6\u5f97\u5c0d\u61c9\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u5f9e\u5217\u8868\u4e2d\u53d6\u5f97\u7d22\u5f15\u503c\u70ba 3 \u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3, 4, 5]\nelement = my_list[3]  # \u4f7f\u7528\u7d22\u5f15\u76f4\u63a5\u53d6\u503c<br \/>\nprint(element)  # \u8f38\u51fa: 4<br \/>\n&#8220;`<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c`__getitem__()` \u5176\u5be6\u662f\u900f\u904e\u65b9\u62ec\u865f `[]` \u4f86\u8abf\u7528\u7684\uff0c\u9019\u662f Python \u5c0d\u5e95\u5c64\u65b9\u6cd5\u7684\u5c01\u88dd\u3002\u57f7\u884c\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\uff0c\u53ef\u4ee5\u5f97\u5230\u5217\u8868\u4e2d\u7d22\u5f15\u503c\u70ba 3 \u7684\u5143\u7d20\uff0c\u5373 4\u3002<\/p>\n<h2>__getitem__() \u5728\u5b57\u5178\u4e2d\u7684\u61c9\u7528<\/h2>\n<p>`__getitem__()` \u65b9\u6cd5\u5728\u5b57\u5178\u4e2d\u540c\u6a23\u9069\u7528\uff0c\u53ef\u4ee5\u7528\u65bc\u53d6\u5f97\u7279\u5b9a\u9375\u6240\u5c0d\u61c9\u7684\u503c\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u53d6\u5f97\u5b57\u5178\u4e2d\u9375\u70ba &#8220;name&#8221; \u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7684\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_dict = {<br \/>\n    &#8220;name&#8221;: &#8220;John&#8221;,<br \/>\n    &#8220;age&#8221;: 20<br \/>\n}<br \/>\nname = my_dict[&#8220;name&#8221;]  # \u4f7f\u7528\u9375\u76f4\u63a5\u53d6\u503c<br \/>\nprint(name)  # \u8f38\u51fa: John<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u5beb\u6cd5\u4f7f\u5f97\u8cc7\u6599\u5b58\u53d6\u66f4\u52a0\u76f4\u89c0\u8207\u7c21\u6f54\u3002<\/p>\n<h2>\u591a\u7dad\u5ea6\u8cc7\u6599\u7d50\u69cb\u7684\u5b58\u53d6<\/h2>\n<p>`__getitem__()` \u4e5f\u53ef\u4ee5\u7528\u65bc\u591a\u7dad\u5ea6\u7684\u53ef\u8fed\u4ee3\u7269\u4ef6\u4e2d\uff0c\u4f8b\u5982\u4e8c\u7dad\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u53d6\u5f97\u4e8c\u7dad\u5217\u8868\u4e2d\u67d0\u500b\u5143\u7d20\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\nelement = my_matrix[1][2]  # \u53d6\u5f97\u7b2c\u4e8c\u884c\u7b2c\u4e09\u5217\u7684\u5143\u7d20<br \/>\nprint(element)  # \u8f38\u51fa: 6<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u65b9\u5f0f\u53ef\u4ee5\u8f15\u9b06\u5730\u5b58\u53d6\u5230\u8907\u96dc\u8cc7\u6599\u7d50\u69cb\u4e2d\u7684\u7279\u5b9a\u5143\u7d20\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u4f7f\u7528 `__getitem__()` \u65b9\u6cd5\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e7e\u7a2e\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u7d22\u5f15\u932f\u8aa4\uff08IndexError\uff09**\uff1a\u7576\u7d22\u5f15\u8d85\u51fa\u7bc4\u570d\u6642\uff0c\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u7d22\u5f15\u503c\u5728\u6709\u6548\u7bc4\u570d\u5167\u3002<br \/>\n2. **\u9375\u932f\u8aa4\uff08KeyError\uff09**\uff1a\u7576\u67e5\u8a62\u7684\u9375\u5728\u5b57\u5178\u4e2d\u4e0d\u5b58\u5728\u6642\uff0c\u5c07\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u53ef\u4ee5\u4f7f\u7528 `dict.get()` \u65b9\u6cd5\u4f86\u907f\u514d\u6b64\u554f\u984c\u3002<\/p>\n<p>\u6700\u4f73\u5be6\u8e10\u5efa\u8b70\uff1a<br \/>\n&#8211; \u4f7f\u7528 `try-except` \u584a\u4f86\u6355\u7372\u53ef\u80fd\u7684\u932f\u8aa4\uff0c\u4ee5\u63d0\u5347\u7a0b\u5f0f\u7684\u5065\u58ef\u6027\u3002<br \/>\n&#8211; \u5118\u91cf\u4f7f\u7528\u660e\u78ba\u7684\u7d22\u5f15\u6216\u9375\u540d\u4f86\u589e\u52a0\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`__getitem__()` \u65b9\u6cd5\u4e0d\u50c5\u9650\u65bc\u57fa\u672c\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u4e5f\u53ef\u4ee5\u7528\u65bc\u81ea\u5b9a\u7fa9\u985e\u5225\u4e2d\u3002\u4f60\u53ef\u4ee5\u901a\u904e\u5be6\u4f5c `__getitem__()` \u65b9\u6cd5\uff0c\u8b93\u81ea\u5b9a\u7fa9\u985e\u5225\u5177\u6709\u985e\u4f3c\u5217\u8868\u6216\u5b57\u5178\u7684\u7d22\u5f15\u5b58\u53d6\u884c\u70ba\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass CustomList:<br \/>\n    def __init__(self, initial_data):<br \/>\n        self.data = initial_data<\/p>\n<p>    def __getitem__(self, index):<br \/>\n        return self.data[index]\n<p>custom_list = CustomList([10, 20, 30])<br \/>\nprint(custom_list[1])  # \u8f38\u51fa: 20<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u65b9\u5f0f\u4f7f\u5f97\u4f60\u7684\u985e\u5225\u53ef\u4ee5\u66f4\u52a0\u9748\u6d3b\u8207\u6613\u65bc\u4f7f\u7528\u3002<\/p>\n<h2>\u53c3\u8003\u8cc7\u6599<\/h2>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/3\/reference\/datamodel.html#object.__getitem__\" target=\"_blank\" rel=\"noopener\">Python 3 \u6587\u4ef6 &#8211; __getitem__()<\/a><\/li>\n<li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/datastructures.html#more-on-lists\" target=\"_blank\" rel=\"noopener\">Python 3 \u6559\u5b78 &#8211; \u66f4\u591a\u95dc\u65bc\u5217\u8868<\/a><\/li>\n<\/ul>\n<h2>\u7d50\u8a9e<\/h2>\n<p>`__getitem__()` \u65b9\u6cd5\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u8b93\u4f60\u80fd\u5920\u5f9e\u5404\u7a2e\u53ef\u8fed\u4ee3\u7269\u4ef6\u4e2d\u5feb\u901f\u53d6\u5f97\u5143\u7d20\u3002\u4e86\u89e3\u5176\u7528\u6cd5\u8207\u61c9\u7528\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u4f60\u7684 Python \u7a0b\u5f0f\u8a2d\u8a08\u80fd\u529b\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `__getitem__()` \u65b9\u6cd5\u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u8cc7\u6599\u7d50\u69cb\uff1f**<br \/>\nA1: `__getitem__()` \u65b9\u6cd5\u53ef\u4ee5\u7528\u65bc\u5217\u8868\u3001\u5b57\u5178\u3001\u5143\u7d44\u7b49\u591a\u7a2e\u53ef\u8fed\u4ee3\u8cc7\u6599\u7d50\u69cb\uff0c\u751a\u81f3\u53ef\u4ee5\u64f4\u5c55\u5230\u81ea\u5b9a\u7fa9\u985e\u5225\u4e2d\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u907f\u514d\u4f7f\u7528 `__getitem__()` \u6642\u51fa\u73fe\u7684\u932f\u8aa4\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u4f7f\u7528 `try-except` \u584a\u4f86\u6355\u7372\u53ef\u80fd\u7684\u932f\u8aa4\uff0c\u4e26\u4f7f\u7528 `dict.get()` \u65b9\u6cd5\u4f86\u5b89\u5168\u5730\u8a2a\u554f\u5b57\u5178\u5143\u7d20\u3002<\/p>\n<p>**Q3: `__getitem__()` \u65b9\u6cd5\u80fd\u5426\u7528\u65bc\u591a\u7dad\u6578\u64da\u7d50\u69cb\uff1f**<br \/>\nA3: \u662f\u7684\uff0c`__getitem__()` \u65b9\u6cd5\u53ef\u4ee5\u7528\u65bc\u591a\u7dad\u6578\u64da\u7d50\u69cb\uff0c\u5982\u4e8c\u7dad\u5217\u8868\uff0c\u901a\u904e\u5d4c\u5957\u7d22\u5f15\u4f86\u8a2a\u554f\u5143\u7d20\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u6df1\u5c64\u77e5\u8b58\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u6559\u5b78\u6587](https:\/\/vocus.cc\/article\/601b5d724b51f80001d5b9f6) \u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684getitem()\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5f9e\u5e8f\u5217\u4e2d\u53d6\u5f97\u7279\u5b9a\u5143\u7d20\uff0c\u4e26\u63d0\u4f9b\u7c21\u55ae\u7684\u65b9\u6cd5\u4f86\u8a2a\u554f\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u5730\u8655\u7406\u5e8f\u5217\u8cc7\u6599\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-3448","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-TC","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3448","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=3448"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3448\/revisions"}],"predecessor-version":[{"id":12829,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3448\/revisions\/12829"}],"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=3448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}