{"id":3128,"date":"2025-05-28T16:39:44","date_gmt":"2025-06-04T08:51:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3128"},"modified":"2025-06-04T16:39:44","modified_gmt":"2025-06-04T08:51:43","slug":"python%e4%b8%ad%e7%9a%84class_setitem%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84class_setitem%e5%8a%9f%e8%83%bd\/","title":{"rendered":"2025 \u6700\u65b0\u7248 Python \u4e2d\u7684 `__setitem__` \u65b9\u6cd5\u4f7f\u7528\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __setitem__, \u985e\u5225\u5c6c\u6027\u8a2d\u5b9a, Python \u6559\u5b78\"><\/p>\n<h1>Python \u4e2d\u7684 `__setitem__` \u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__setitem__` \u662f\u4e00\u500b\u7279\u6b8a\u65b9\u6cd5\uff0c\u5141\u8a31\u6211\u5011\u4f7f\u7528\u65b9\u62ec\u865f\u8a9e\u6cd5\u4f86\u8a2d\u7f6e\u985e\u5225\u7684\u5c6c\u6027\u3002\u9019\u500b\u65b9\u6cd5\u4e0d\u50c5\u8b93\u6211\u5011\u7684\u4ee3\u78bc\u66f4\u52a0\u7c21\u6f54\uff0c\u4e5f\u63d0\u9ad8\u4e86\u53ef\u8b80\u6027\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `__setitem__` \u7684\u7528\u6cd5\u3001\u5be6\u4f5c\u7bc4\u4f8b\u4ee5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u4f60\u5728 Python \u7a0b\u5f0f\u8a2d\u8a08\u4e2d\u9748\u6d3b\u904b\u7528\u9019\u4e00\u529f\u80fd\u3002<\/p>\n<p>## `__setitem__` \u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>`__setitem__` \u65b9\u6cd5\u7684\u5b9a\u7fa9\u53ef\u4ee5\u63a5\u6536\u5169\u500b\u53c3\u6578\uff1a\u7b2c\u4e00\u500b\u662f\u5c6c\u6027\u540d\u7a31\uff0c\u7b2c\u4e8c\u500b\u662f\u5c6c\u6027\u503c\u3002\u4f7f\u7528\u9019\u500b\u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u901a\u904e\u7c21\u55ae\u7684\u65b9\u62ec\u865f\u8a9e\u6cd5\u4f86\u8a2d\u7f6e\u985e\u5225\u7684\u5c6c\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self):<br \/>\n        self.attributes = {}<\/p>\n<p>    def __setitem__(self, key, value):<br \/>\n        self.attributes[key] = value<\/p>\n<p>my_class = MyClass()<br \/>\nmy_class[&#8216;name&#8217;] = &#8216;John&#8217;<\/p>\n<p>print(my_class.attributes[&#8216;name&#8217;])  # John<br \/>\n&#8220;`<\/p>\n<p>## \u8a2d\u7f6e\u591a\u500b\u5c6c\u6027<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u5728\u985e\u5225\u4e2d\u8a2d\u5b9a\u591a\u500b\u5c6c\u6027\uff0c\u53ef\u4ee5\u5c07\u5b83\u5011\u653e\u5728\u5b57\u5178\u4e2d\uff0c\u7136\u5f8c\u4f9d\u6b21\u8a2d\u7f6e\u3002\u9019\u6a23\u53ef\u4ee5\u8b93\u4f60\u7684\u4ee3\u78bc\u66f4\u52a0\u6574\u6f54\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self):<br \/>\n        self.attributes = {}<\/p>\n<p>    def __setitem__(self, key, value):<br \/>\n        self.attributes[key] = value<\/p>\n<p>my_class = MyClass()<br \/>\nmy_class[&#8216;name&#8217;] = &#8216;John&#8217;<br \/>\nmy_class[&#8216;age&#8217;] = 20<\/p>\n<p>print(my_class.attributes[&#8216;name&#8217;])  # John<br \/>\nprint(my_class.attributes[&#8216;age&#8217;])   # 20<br \/>\n&#8220;`<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `__setitem__` \u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u932f\u8aa4\uff0c\u4f8b\u5982\u5617\u8a66\u8a2d\u7f6e\u672a\u5b9a\u7fa9\u7684\u5c6c\u6027\u3002\u70ba\u4e86\u907f\u514d\u9019\u4e9b\u932f\u8aa4\uff0c\u6211\u5011\u53ef\u4ee5\u5728 `__setitem__` \u65b9\u6cd5\u4e2d\u589e\u52a0\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self):<br \/>\n        self.attributes = {}<\/p>\n<p>    def __setitem__(self, key, value):<br \/>\n        if not isinstance(key, str):<br \/>\n            raise ValueError(&#8220;\u5c6c\u6027\u540d\u7a31\u5fc5\u9808\u662f\u5b57\u4e32&#8221;)<br \/>\n        self.attributes[key] = value<\/p>\n<p>my_class = MyClass()<br \/>\ntry:<br \/>\n    my_class[1] = &#8216;Invalid&#8217;  # \u9019\u5c07\u5f15\u767c\u932f\u8aa4<br \/>\nexcept ValueError as e:<br \/>\n    print(e)  # \u5c6c\u6027\u540d\u7a31\u5fc5\u9808\u662f\u5b57\u4e32<br \/>\n&#8220;`<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`__setitem__` \u65b9\u6cd5\u7684\u61c9\u7528\u4e0d\u50c5\u9650\u65bc\u7c21\u55ae\u7684\u5c6c\u6027\u8a2d\u5b9a\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u5275\u5efa\u66f4\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u5982\u5b57\u5178\u6216\u5217\u8868\u7684\u5c01\u88dd\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5229\u7528 `__setitem__` \u65b9\u6cd5\u4f86\u5275\u5efa\u4e00\u500b\u7c21\u55ae\u7684\u6a21\u64ec\u8cc7\u6599\u5eab\uff0c\u5132\u5b58\u548c\u6aa2\u7d22\u8cc7\u6599\u3002<\/p>\n<p>## \u7d50\u8ad6<\/p>\n<p>`__setitem__` \u65b9\u6cd5\u662f Python \u4e2d\u4e00\u500b\u5f37\u5927\u7684\u529f\u80fd\uff0c\u80fd\u5920\u5e6b\u52a9\u4f60\u66f4\u65b9\u4fbf\u5730\u8a2d\u7f6e\u985e\u5225\u7684\u5c6c\u6027\u3002\u900f\u904e\u6b63\u78ba\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u4f60\u53ef\u4ee5\u63d0\u5347\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u7dad\u8b77\u6027\u3002\u5982\u679c\u4f60\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u9032\u968e\u7528\u6cd5\uff0c\u5efa\u8b70\u53c3\u8003\u9019\u7bc7\u6559\u5b78 [Python \u9032\u968e\u6559\u5b78](https:\/\/vocus.cc\/article\/62d0646f4e2c2500643c7f8a)\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `__setitem__` \u65b9\u6cd5\u548c\u666e\u901a\u7684\u5c6c\u6027\u8a2d\u5b9a\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `__setitem__` \u4f7f\u7528\u65b9\u62ec\u865f\u8a9e\u6cd5\u4f86\u8a2d\u7f6e\u5c6c\u6027\uff0c\u9019\u4f7f\u5f97\u4ee3\u78bc\u770b\u8d77\u4f86\u66f4\u7c21\u6f54\u4e14\u6613\u65bc\u7406\u89e3\u3002\u800c\u666e\u901a\u7684\u5c6c\u6027\u8a2d\u5b9a\u9700\u8981\u4f7f\u7528\u9ede\u8a9e\u6cd5\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728 `__setitem__` \u4e2d\u6dfb\u52a0\u66f4\u591a\u7684\u9a57\u8b49\u908f\u8f2f\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u5728 `__setitem__` \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u689d\u4ef6\u8a9e\u53e5\uff0c\u6aa2\u67e5\u6578\u64da\u985e\u578b\u6216\u5176\u4ed6\u689d\u4ef6\uff0c\u4ee5\u78ba\u4fdd\u8a2d\u7f6e\u7684\u5c6c\u6027\u503c\u7b26\u5408\u9810\u671f\u3002<\/p>\n<p>**Q3: \u662f\u5426\u53ef\u4ee5\u91cd\u8f09\u5176\u4ed6\u7279\u6b8a\u65b9\u6cd5\uff1f**<br \/>\nA3: \u662f\u7684\uff0cPython \u652f\u6301\u91cd\u8f09\u591a\u7a2e\u7279\u6b8a\u65b9\u6cd5\uff0c\u4f8b\u5982 `__getitem__`\u3001`__delitem__` \u7b49\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u8981\u4f86\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u884c\u70ba\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>class_setitem()\u662fPython\u4e2d\u7684\u4e00\u500b\u529f\u80fd\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u8a2d\u7f6e\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u5f48\u6027\uff0c\u8b93\u958b\u767c\u8005\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-3128","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-Os","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3128","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=3128"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3128\/revisions"}],"predecessor-version":[{"id":3129,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3128\/revisions\/3129"}],"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=3128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}