{"id":3518,"date":"2025-05-28T15:13:07","date_gmt":"2025-06-04T07:24:06","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3518"},"modified":"2025-06-04T15:13:07","modified_gmt":"2025-06-04T07:24:06","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%adslots%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e5%84%aa%e9%bb%9e","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%adslots%e5%87%bd%e6%95%b8%e7%9a%84%e5%8a%9f%e8%83%bd%e8%88%87%e5%84%aa%e9%bb%9e\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u7684 `__slots__` \u51fd\u6578\uff1a\u529f\u80fd\u3001\u512a\u9ede\u8207\u4f7f\u7528\u7bc4\u4f8b\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, Slots, __slots__\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u7684 `__slots__` \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__slots__` \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u9650\u5236\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u5f9e\u800c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u54c1\u8cea\u548c\u6548\u80fd\u3002\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e `__slots__` \u7684\u529f\u80fd\u3001\u512a\u9ede\uff0c\u4ee5\u53ca\u4f7f\u7528\u7bc4\u4f8b\uff0c\u8b93\u4f60\u80fd\u5920\u66f4\u597d\u5730\u638c\u63e1\u9019\u9805\u6280\u8853\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Python \u7684 `__slots__`\uff1f<\/h2>\n<p>`__slots__` \u662f\u4e00\u500b\u7279\u6b8a\u7684\u5c6c\u6027\uff0c\u7528\u65bc\u5b9a\u7fa9\u4e00\u500b\u985e\u5225\u6240\u5141\u8a31\u7684\u5c6c\u6027\u3002\u900f\u904e\u4f7f\u7528 `__slots__`\uff0c\u53ef\u4ee5\u8b93 Python \u7684\u7269\u4ef6\u50c5\u5305\u542b\u7279\u5b9a\u7684\u5c6c\u6027\uff0c\u5f9e\u800c\u907f\u514d\u52d5\u614b\u6dfb\u52a0\u5c6c\u6027\uff0c\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u4e5f\u6709\u6548\u964d\u4f4e\u4e86\u8a18\u61b6\u9ad4\u7684\u4f7f\u7528\u91cf\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Python \u7684 `__slots__`<\/h2>\n<p>\u8981\u4f7f\u7528 `__slots__`\uff0c\u4f60\u9700\u8981\u5728\u985e\u5225\u4e2d\u5b9a\u7fa9\u9019\u500b\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b\u5b57\u4e32\u5217\u8868\uff0c\u6307\u5b9a\u5141\u8a31\u7684\u5c6c\u6027\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u985e\u5225\u4e2d\u4f7f\u7528 `__slots__` \u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    __slots__ = [&#8216;name&#8217;, &#8216;age&#8217;]\n<p>    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `Person` \u985e\u5225\uff0c\u4e26\u4f7f\u7528 `__slots__` \u9650\u5236\u53ea\u6709 `name` \u548c `age` \u9019\u5169\u500b\u5c6c\u6027\u3002\u82e5\u5617\u8a66\u70ba\u6b64\u7269\u4ef6\u6dfb\u52a0\u5176\u4ed6\u5c6c\u6027\uff08\u4f8b\u5982 `height`\uff09\uff0c\u5c07\u6703\u5f15\u767c\u932f\u8aa4\uff1a<\/p>\n<p>&#8220;`python<br \/>\nperson = Person(&#8216;John&#8217;, 30)<br \/>\nperson.height = 180  # AttributeError: &#8216;Person&#8217; object has no attribute &#8216;height&#8217;<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u7269\u4ef6\u7684\u5c6c\u6027\u4e0d\u6703\u88ab\u96a8\u610f\u4fee\u6539\uff0c\u5f9e\u800c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u7a69\u5b9a\u6027\u3002<\/p>\n<h2>Python `__slots__` \u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `__slots__` \u5e36\u4f86\u7684\u512a\u9ede\u5305\u62ec\uff1a<\/p>\n<p>1. **\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u54c1\u8cea**\uff1a\u5f37\u5236\u9650\u5236\u7269\u4ef6\u5c6c\u6027\uff0c\u6e1b\u5c11\u932f\u8aa4\u767c\u751f\u7684\u6a5f\u6703\u3002<br \/>\n2. **\u964d\u4f4e\u8a18\u61b6\u9ad4\u4f7f\u7528\u91cf**\uff1a\u6bcf\u500b\u7269\u4ef6\u7684\u5c6c\u6027\u5b58\u5132\u65b9\u5f0f\u7531\u5b57\u5178\u6539\u70ba\u975c\u614b\u6578\u7d44\uff0c\u6e1b\u5c11\u8a18\u61b6\u9ad4\u958b\u92b7\uff0c\u7279\u5225\u662f\u5728\u5275\u5efa\u5927\u91cf\u7269\u4ef6\u6642\u3002<br \/>\n3. **\u63d0\u5347\u6548\u80fd**\uff1a\u5c0d\u65bc\u5927\u898f\u6a21\u7684\u61c9\u7528\u7a0b\u5f0f\uff0c`__slots__` \u53ef\u4ee5\u6539\u5584\u8a2a\u554f\u5c6c\u6027\u7684\u901f\u5ea6\u3002<\/p>\n<h2>Python `__slots__` \u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u66f4\u5b8c\u6574\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `__slots__` \u9032\u884c\u5c0d\u7269\u4ef6\u5c6c\u6027\u7684\u63a7\u5236\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    __slots__ = [&#8216;name&#8217;, &#8216;age&#8217;]\n<p>    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<\/p>\n<p># \u5275\u5efa Person \u7269\u4ef6<br \/>\nperson = Person(&#8216;Alice&#8217;, 28)<br \/>\nprint(person.name)  # \u8f38\u51fa: Alice<br \/>\nprint(person.age)   # \u8f38\u51fa: 28<\/p>\n<p># \u5617\u8a66\u6dfb\u52a0\u4e0d\u5728 __slots__ \u4e2d\u7684\u5c6c\u6027<br \/>\ntry:<br \/>\n    person.height = 170  # \u5c07\u5f15\u767c AttributeError<br \/>\nexcept AttributeError as e:<br \/>\n    print(e)  # \u8f38\u51fa: &#8216;Person&#8217; object has no attribute &#8216;height&#8217;<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u7bc4\u4f8b\u5c55\u793a\u4e86 `__slots__` \u5982\u4f55\u6709\u6548\u9650\u5236\u7269\u4ef6\u7684\u5c6c\u6027\uff0c\u4e26\u63d0\u4f9b\u4e86\u6e05\u6670\u7684\u932f\u8aa4\u63d0\u793a\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u900f\u904e `__slots__`\uff0cPython \u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u63a7\u5236\u7269\u4ef6\u5c6c\u6027\uff0c\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u54c1\u8cea\u8207\u6548\u80fd\u3002\u5982\u679c\u4f60\u5e0c\u671b\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u7d50\u69cb\u6027\u548c\u6548\u80fd\uff0c\u4f7f\u7528 `__slots__` \u662f\u4e00\u500b\u975e\u5e38\u597d\u7684\u9078\u64c7\u3002\u4e86\u89e3\u66f4\u591a Python \u7684\u6700\u4f73\u5be6\u8e10\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1](https:\/\/miner.tw\/python-best-practices)\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<p>**Q1: \u4f7f\u7528 `__slots__` \u6703\u5f71\u97ff\u7269\u4ef6\u7684\u7e7c\u627f\u55ce\uff1f**<br \/>\nA1: \u662f\u7684\uff0c\u4f7f\u7528 `__slots__` \u7684\u5b50\u985e\u5225\u4e0d\u6703\u81ea\u52d5\u7e7c\u627f\u7236\u985e\u5225\u7684 `__slots__`\uff0c\u9700\u8981\u5728\u5b50\u985e\u5225\u4e2d\u91cd\u65b0\u5b9a\u7fa9\u3002<\/p>\n<p>**Q2: `__slots__` \u662f\u5426\u652f\u6301\u591a\u91cd\u7e7c\u627f\uff1f**<br \/>\nA2: \u662f\u7684\uff0c\u4f46\u5728\u4f7f\u7528\u591a\u91cd\u7e7c\u627f\u6642\uff0c\u5fc5\u9808\u8b39\u614e\u7ba1\u7406 `__slots__` \u7684\u5b9a\u7fa9\uff0c\u907f\u514d\u885d\u7a81\u3002<\/p>\n<p>**Q3: \u4f7f\u7528 `__slots__` \u662f\u5426\u6703\u5f71\u97ff\u6027\u80fd\uff1f**<br \/>\nA3: \u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528 `__slots__` \u6703\u63d0\u9ad8\u6027\u80fd\uff0c\u5c24\u5176\u662f\u7576\u9700\u8981\u5275\u5efa\u5927\u91cf\u7269\u4ef6\u6642\uff0c\u56e0\u70ba\u5b83\u6e1b\u5c11\u4e86\u8a18\u61b6\u9ad4\u7684\u4f7f\u7528\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2dSlots()\u51fd\u6578\u7684\u529f\u80fd\uff0cSlots()\u51fd\u6578\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u4f7f\u7528Python\uff0c\u4e26\u4e14\u53ef\u4ee5\u6e1b\u5c11\u8a18\u61b6\u9ad4\u7684\u4f7f\u7528\u91cf\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u66f4\u52a0\u9ad8\u6548\u7387\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-3518","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-UK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3518","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=3518"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3518\/revisions"}],"predecessor-version":[{"id":12811,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3518\/revisions\/12811"}],"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=3518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}