{"id":3124,"date":"2025-05-29T16:36:05","date_gmt":"2025-06-04T08:51:05","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3124"},"modified":"2025-06-04T16:36:05","modified_gmt":"2025-06-04T08:51:05","slug":"python%e4%b8%ad%e7%9a%84init_subclass%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84init_subclass%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u6df1\u5165\u63a2\u8a0e Python \u4e2d\u7684 `init_subclass()` \u65b9\u6cd5\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, init_subclass, Python \u7a0b\u5f0f\u6559\u5b78, Python \u985e\u5225\u8a2d\u8a08\"><\/p>\n<h1>\u6df1\u5165\u63a2\u8a0e Python \u4e2d\u7684 `init_subclass()` \u65b9\u6cd5\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u7684\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u4e2d\uff0c`init_subclass()` \u662f\u4e00\u500b\u5f37\u5927\u7684\u65b9\u6cd5\uff0c\u5141\u8a31\u958b\u767c\u8005\u5728\u5b50\u985e\u5225\u88ab\u5275\u5efa\u6642\u81ea\u52d5\u57f7\u884c\u67d0\u4e9b\u884c\u70ba\uff0c\u800c\u4e0d\u6703\u5f71\u97ff\u5230\u7236\u985e\u5225\u7684\u884c\u70ba\u3002\u9019\u500b\u65b9\u6cd5\u5728\u5efa\u7acb\u53ef\u64f4\u5c55\u7684\u985e\u5225\u67b6\u69cb\u6642\u7279\u5225\u6709\u7528\uff0c\u5c24\u5176\u5728\u9700\u8981\u5c0d\u5b50\u985e\u5225\u9032\u884c\u7279\u5b9a\u914d\u7f6e\u7684\u60c5\u6cc1\u4e0b\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f `init_subclass()` \u65b9\u6cd5\uff1f<\/h2>\n<p>`init_subclass()` \u662f\u4e00\u500b\u985e\u5225\u65b9\u6cd5\uff0c\u7576\u5b50\u985e\u5225\u88ab\u5275\u5efa\u6642\u6703\u81ea\u52d5\u8abf\u7528\u3002\u9019\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u5728\u5b50\u985e\u5225\u4e2d\u81ea\u52d5\u5b9a\u7fa9\u65b9\u6cd5\u6216\u5c6c\u6027\uff0c\u800c\u7121\u9700\u5728\u6bcf\u500b\u5b50\u985e\u5225\u4e2d\u91cd\u8907\u7de8\u5beb\u76f8\u540c\u7684\u4ee3\u78bc\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u91cd\u7528\u6027\uff0c\u4e5f\u4f7f\u5f97\u985e\u5225\u8a2d\u8a08\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<h2>\u57fa\u672c\u7528\u6cd5\u793a\u7bc4<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 `init_subclass()` \u7684\u57fa\u672c\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u4e00\u500b\u65b0\u65b9\u6cd5\u800c\u4e0d\u5f71\u97ff\u7236\u985e\u5225\u7684\u884c\u70ba\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Parent:<br \/>\n    def say_hello(self):<br \/>\n        print(&#8220;Hello!&#8221;)<\/p>\n<p>class Child(Parent):<br \/>\n    @classmethod<br \/>\n    def __init_subclass__(cls):<br \/>\n        super().__init_subclass__()  # \u78ba\u4fdd\u8abf\u7528\u7236\u985e\u5225\u7684 init_subclass<br \/>\n        def say_goodbye(self):<br \/>\n            print(&#8220;Goodbye!&#8221;)<br \/>\n        cls.say_goodbye = say_goodbye<\/p>\n<p># \u6e2c\u8a66<br \/>\nchild_instance = Child()<br \/>\nchild_instance.say_hello()  # \u8f38\u51fa: Hello!<br \/>\nchild_instance.say_goodbye()  # \u8f38\u51fa: Goodbye!<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5728 `Child` \u985e\u5225\u4e2d\u4f7f\u7528 `init_subclass()` \u4f86\u589e\u52a0\u4e00\u500b\u65b0\u7684\u65b9\u6cd5 `say_goodbye()`\uff0c\u9019\u500b\u65b9\u6cd5\u53ea\u5b58\u5728\u65bc\u5b50\u985e\u5225\u4e2d\uff0c\u800c\u4e0d\u5f71\u97ff `Parent` \u985e\u5225\u7684\u884c\u70ba\u3002<\/p>\n<h2>\u4f7f\u7528 `init_subclass()` \u7684\u512a\u9ede<\/h2>\n<p>1. **\u4ee3\u78bc\u91cd\u7528**\uff1a\u4f7f\u7528 `init_subclass()` \u53ef\u4ee5\u907f\u514d\u5728\u6bcf\u500b\u5b50\u985e\u5225\u4e2d\u91cd\u8907\u76f8\u540c\u7684\u4ee3\u78bc\u3002<br \/>\n2. **\u63d0\u9ad8\u53ef\u64f4\u5c55\u6027**\uff1a\u96a8\u8457\u61c9\u7528\u7a0b\u5f0f\u7684\u589e\u9577\uff0c\u80fd\u5920\u8f15\u9b06\u5730\u5411\u6240\u6709\u5b50\u985e\u5225\u6dfb\u52a0\u65b0\u529f\u80fd\u3002<br \/>\n3. **\u660e\u78ba\u7684\u985e\u5225\u7d50\u69cb**\uff1a\u4f7f\u5f97\u985e\u5225\u4e4b\u9593\u7684\u95dc\u4fc2\u66f4\u52a0\u6e05\u6670\uff0c\u6613\u65bc\u7dad\u8b77\u3002<\/p>\n<h2>\u5be6\u4f5c\u4e2d\u7684\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `init_subclass()` \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; **\u672a\u8abf\u7528\u7236\u985e\u5225\u7684 `init_subclass()`**\uff1a\u5982\u679c\u5fd8\u8a18\u8abf\u7528 `super().__init_subclass__()`\uff0c\u7236\u985e\u5225\u7684\u521d\u59cb\u5316\u884c\u70ba\u53ef\u80fd\u7121\u6cd5\u6b63\u78ba\u57f7\u884c\u3002<br \/>\n&#8211; **\u65b9\u6cd5\u547d\u540d\u885d\u7a81**\uff1a\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u7684\u65b9\u6cd5\u540d\u7a31\u5982\u679c\u8207\u7236\u985e\u5225\u76f8\u540c\uff0c\u5c07\u6703\u8986\u84cb\u7236\u985e\u5225\u7684\u65b9\u6cd5\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u610f\u5916\u884c\u70ba\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u53ef\u4ee5\u9032\u4e00\u6b65\u4f7f\u7528 `init_subclass()` \u4f86\u81ea\u52d5\u8a3b\u518a\u5b50\u985e\u5225\uff0c\u4f8b\u5982\u5728\u4e00\u500b\u63d2\u4ef6\u7cfb\u7d71\u4e2d\uff0c\u7576\u6bcf\u500b\u5b50\u985e\u5225\u88ab\u5275\u5efa\u6642\uff0c\u53ef\u4ee5\u81ea\u52d5\u5c07\u5176\u8a3b\u518a\u5230\u7cfb\u7d71\u4e2d\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u53ef\u4ee5\u6975\u5927\u5730\u63d0\u9ad8\u7cfb\u7d71\u7684\u9748\u6d3b\u6027\u548c\u64f4\u5c55\u6027\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u7e3d\u800c\u8a00\u4e4b\uff0c`init_subclass()` \u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5728\u5b50\u985e\u5225\u4e2d\u9748\u6d3b\u5b9a\u7fa9\u884c\u70ba\uff0c\u800c\u4e0d\u5f71\u97ff\u7236\u985e\u5225\u7684\u7d50\u69cb\u3002\u6b64\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u7dad\u8b77\u6027\uff0c\u4e5f\u4f7f\u5f97\u985e\u5225\u4e4b\u9593\u7684\u95dc\u4fc2\u66f4\u52a0\u6e05\u6670\u3002<\/p>\n<p>\u82e5\u60f3\u6df1\u5165\u4e86\u89e3\u66f4\u591a Python \u7684\u6559\u5b78\u8207\u5be6\u4f8b\uff0c\u6b61\u8fce\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc\/article\/xxxxxx)\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<p>**Q1: `init_subclass()` \u548c `__init__()` \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: `init_subclass()` \u662f\u5728\u985e\u5225\u5275\u5efa\u6642\u8abf\u7528\u7684\uff0c\u800c `__init__()` \u662f\u5728\u5be6\u4f8b\u5316\u6642\u8abf\u7528\u7684\u3002\u524d\u8005\u4e3b\u8981\u7528\u65bc\u985e\u5225\u5c64\u7d1a\u7684\u914d\u7f6e\uff0c\u5f8c\u8005\u5247\u7528\u65bc\u5be6\u4f8b\u5c64\u7d1a\u7684\u521d\u59cb\u5316\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728 `init_subclass()` \u4e2d\u4f7f\u7528\u53c3\u6578\uff1f**<br \/>\nA2: \u53ef\u4ee5\u5728 `init_subclass()` \u7684\u5b9a\u7fa9\u4e2d\u6dfb\u52a0\u53c3\u6578\uff0c\u7136\u5f8c\u5728\u5275\u5efa\u5b50\u985e\u5225\u6642\u50b3\u905e\u9019\u4e9b\u53c3\u6578\uff0c\u4ee5\u4fbf\u5728\u985e\u5225\u5c64\u7d1a\u9032\u884c\u914d\u7f6e\u3002<\/p>\n<p>**Q3: \u662f\u5426\u53ef\u4ee5\u91cd\u5beb `init_subclass()` \u65b9\u6cd5\uff1f**<br \/>\nA3: \u662f\u7684\uff0c\u5b50\u985e\u5225\u53ef\u4ee5\u91cd\u5beb `init_subclass()` \u65b9\u6cd5\u4f86\u5be6\u73fe\u7279\u5b9a\u7684\u884c\u70ba\u3002\u53ea\u9700\u78ba\u4fdd\u8abf\u7528 `super().__init_subclass__()` \u4ee5\u4fdd\u7559\u7236\u985e\u5225\u7684\u884c\u70ba\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>init_subclass()\u662fPython\u4e2d\u4e00\u7a2e\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7528\u65bc\u5728\u5b50\u985e\u5225\u4e2d\u521d\u59cb\u5316\u8a2d\u5b9a\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u3002\u672c\u6587\u5c07\u4ecb\u7d39init_subclass()\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u6539\u5584\u60a8\u7684Python\u7a0b\u5e8f\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-3124","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-Oo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3124","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=3124"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3124\/revisions"}],"predecessor-version":[{"id":3125,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3124\/revisions\/3125"}],"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=3124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}