{"id":3146,"date":"2025-05-28T16:23:48","date_gmt":"2025-06-04T08:46:48","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3146"},"modified":"2025-06-04T16:23:48","modified_gmt":"2025-06-04T08:46:48","slug":"python%e4%b8%ad%e7%9a%84init_subclass%e6%96%b9%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84init_subclass%e6%96%b9%e6%b3%95-2\/","title":{"rendered":"Python\u4e2d\u7684 `init_subclass()` \u65b9\u6cd5\uff1a\u5b8c\u6574\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, init_subclass, Python \u6559\u5b78, Python \u7e7c\u627f\"><\/p>\n<h1>Python \u4e2d\u7684 `init_subclass()` \u65b9\u6cd5<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`init_subclass()` \u662f\u4e00\u500b\u5f37\u5927\u7684\u65b9\u6cd5\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u5728\u5b50\u985e\u5225\u4e2d\u5b9a\u7fa9\u7279\u5b9a\u884c\u70ba\uff0c\u800c\u4e0d\u5f71\u97ff\u7236\u985e\u5225\u7684\u884c\u70ba\u3002\u9019\u4f7f\u5f97 Python \u7684\u7269\u4ef6\u5c0e\u5411\u8a2d\u8a08\u66f4\u52a0\u9748\u6d3b\u548c\u53ef\u64f4\u5c55\u3002\u672c\u6587\u5c07\u63d0\u4f9b `init_subclass()` \u65b9\u6cd5\u7684\u6700\u65b0\u8a9e\u6cd5\u53ca\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u6db5\u84cb\u5be6\u4f5c\u7bc4\u4f8b\u3001\u932f\u8aa4\u6392\u9664\u548c\u5ef6\u4f38\u61c9\u7528\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u4ec0\u9ebc\u662f `init_subclass()`\uff1f<\/h2>\n<p>`init_subclass()` \u662f\u4e00\u500b\u985e\u5225\u65b9\u6cd5\uff0c\u7576\u4e00\u500b\u985e\u5225\u88ab\u5275\u5efa\u6642\uff0c\u9019\u500b\u65b9\u6cd5\u6703\u88ab\u81ea\u52d5\u8abf\u7528\u3002\u9019\u610f\u5473\u8457\u6bcf\u7576\u4f60\u5275\u5efa\u4e00\u500b\u5b50\u985e\u5225\u6642\uff0c\u90fd\u53ef\u4ee5\u5728\u9019\u88e1\u57f7\u884c\u7279\u5b9a\u7684\u521d\u59cb\u5316\u908f\u8f2f\u3002\u9019\u5c0d\u65bc\u9700\u8981\u81ea\u52d5\u8a2d\u7f6e\u6216\u9a57\u8b49\u5b50\u985e\u5225\u7684\u60c5\u5883\u7279\u5225\u6709\u7528\u3002<\/p>\n<h2>2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u662f `init_subclass()` \u65b9\u6cd5\u7684\u57fa\u672c\u7528\u6cd5\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Parent:<br \/>\n    def __init__(self):<br \/>\n        self.name = &#8216;Parent&#8217;<\/p>\n<p>    @classmethod<br \/>\n    def init_subclass(cls):<br \/>\n        print(f'{cls.__name__} class is initialized&#8217;)<\/p>\n<p>class Child(Parent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()<br \/>\n        self.name = &#8216;Child&#8217;<\/p>\n<p># \u5275\u5efa Child \u985e\u5225\u7684\u5be6\u4f8b<br \/>\nchild_instance = Child()<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `Parent` \u985e\u5225\u548c\u4e00\u500b `Child` \u985e\u5225\u3002\u7576 `Child` \u985e\u5225\u88ab\u5b9a\u7fa9\u6642\uff0c`init_subclass()` \u65b9\u6cd5\u6703\u88ab\u81ea\u52d5\u8abf\u7528\uff0c\u4e26\u8f38\u51fa &#8220;Child class is initialized&#8221;\u3002<\/p>\n<h2>\u4f7f\u7528 `init_subclass()` \u7684\u512a\u9ede<\/h2>\n<p>1. **\u9748\u6d3b\u6027**\uff1a\u53ef\u4ee5\u91dd\u5c0d\u4e0d\u540c\u7684\u5b50\u985e\u5225\u57f7\u884c\u4e0d\u540c\u7684\u521d\u59cb\u5316\u908f\u8f2f\uff0c\u800c\u4e0d\u5f71\u97ff\u7236\u985e\u5225\u3002<br \/>\n2. **\u53ef\u8b80\u6027**\uff1a\u53ef\u4ee5\u5c07\u521d\u59cb\u5316\u908f\u8f2f\u96c6\u4e2d\u5728\u4e00\u500b\u5730\u65b9\uff0c\u589e\u52a0\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002<br \/>\n3. **\u81ea\u52d5\u5316**\uff1a\u53ef\u4ee5\u81ea\u52d5\u8655\u7406\u5b50\u985e\u5225\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\uff0c\u6e1b\u5c11\u91cd\u8907\u4ee3\u78bc\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b\uff1a\u52d5\u614b\u6dfb\u52a0\u5c6c\u6027<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u66f4\u9032\u4e00\u6b65\u7684\u793a\u7bc4\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `init_subclass()` \u52d5\u614b\u6dfb\u52a0\u5c6c\u6027\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Base:<br \/>\n    @classmethod<br \/>\n    def init_subclass(cls):<br \/>\n        cls.dynamic_property = f&#8217;Dynamic property of {cls.__name__}&#8217;<\/p>\n<p>class DerivedA(Base):<br \/>\n    pass<\/p>\n<p>class DerivedB(Base):<br \/>\n    pass<\/p>\n<p># \u6aa2\u67e5\u52d5\u614b\u6dfb\u52a0\u7684\u5c6c\u6027<br \/>\nprint(DerivedA.dynamic_property)  # \u8f38\u51fa: Dynamic property of DerivedA<br \/>\nprint(DerivedB.dynamic_property)  # \u8f38\u51fa: Dynamic property of DerivedB<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`DerivedA` \u548c `DerivedB` \u985e\u5225\u6703\u81ea\u52d5\u64c1\u6709\u4e00\u500b\u52d5\u614b\u5c6c\u6027 `dynamic_property`\uff0c\u800c\u9019\u500b\u5c6c\u6027\u662f\u5728 `init_subclass()` \u4e2d\u5b9a\u7fa9\u7684\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>&#8211; **\u78ba\u4fdd\u4f7f\u7528 `@classmethod` \u88dd\u98fe\u5668**\uff1a`init_subclass()` \u5fc5\u9808\u662f\u985e\u5225\u65b9\u6cd5\uff0c\u5426\u5247\u6703\u5f15\u767c\u932f\u8aa4\u3002<br \/>\n&#8211; **\u907f\u514d\u91cd\u8907\u5b9a\u7fa9**\uff1a\u5982\u679c\u591a\u500b\u5b50\u985e\u5225\u9700\u8981\u76f8\u540c\u7684\u884c\u70ba\uff0c\u8003\u616e\u5c07\u9019\u4e9b\u884c\u70ba\u96c6\u4e2d\u5728\u7236\u985e\u5225\u4e2d\uff0c\u800c\u4e0d\u662f\u5728\u6bcf\u500b\u5b50\u985e\u5225\u4e2d\u91cd\u8907\u5b9a\u7fa9\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>&#8211; \u5728\u5927\u578b\u5c08\u6848\u4e2d\uff0c\u4f7f\u7528 `init_subclass()` \u4f86\u7ba1\u7406\u4e0d\u540c\u6a21\u7d44\u7684\u8a2d\u5b9a\u6216\u884c\u70ba\uff0c\u80fd\u5920\u7c21\u5316\u6574\u9ad4\u67b6\u69cb\u3002<br \/>\n&#8211; \u7d50\u5408\u5176\u4ed6\u8a2d\u8a08\u6a21\u5f0f\uff08\u5982\u55ae\u4f8b\u6a21\u5f0f\u6216\u5de5\u5ee0\u6a21\u5f0f\uff09\u4f7f\u7528 `init_subclass()` \u53ef\u4ee5\u63d0\u5347\u4ee3\u78bc\u7684\u9748\u6d3b\u6027\u8207\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u5982\u679c\u60a8\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u9032\u968e\u6280\u5de7\uff0c\u6b61\u8fce\u53c3\u8003\u9019\u7bc7 [Python \u9032\u968e\u6559\u5b78](https:\/\/vocus.cc\/article\/5f3bc6fef2a0fb0010f6e4c1)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `init_subclass()` \u53ef\u4ee5\u7528\u4f86\u505a\u4ec0\u9ebc\uff1f**<br \/>\nA1: `init_subclass()` \u53ef\u4ee5\u7528\u4f86\u5728\u5b50\u985e\u5225\u88ab\u5275\u5efa\u6642\u81ea\u52d5\u57f7\u884c\u521d\u59cb\u5316\u908f\u8f2f\uff0c\u4f8b\u5982\u8a2d\u7f6e\u5c6c\u6027\u3001\u8a3b\u518a\u985e\u5225\u7b49\u3002<\/p>\n<p>**Q2: `init_subclass()` \u662f\u5426\u9700\u8981\u8fd4\u56de\u503c\uff1f**<br \/>\nA2: \u4e0d\u9700\u8981\uff0c`init_subclass()` \u901a\u5e38\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u4e3b\u8981\u7528\u65bc\u57f7\u884c\u521d\u59cb\u5316\u64cd\u4f5c\u3002<\/p>\n<p>**Q3: \u4f55\u6642\u61c9\u8a72\u4f7f\u7528 `init_subclass()`\uff1f**<br \/>\nA3: \u7576\u60a8\u9700\u8981\u5728\u5b50\u985e\u5225\u5275\u5efa\u6642\u81ea\u52d5\u57f7\u884c\u7279\u5b9a\u7684\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u6216\u8005\u9700\u8981\u5c0d\u5b50\u985e\u5225\u9032\u884c\u7279\u5b9a\u7684\u914d\u7f6e\u6642\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `init_subclass()`\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\u7236\u985e\u5225\u7684\u5c6c\u6027\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\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u64f4\u5c55\u6027\u548c\u53ef\u7dad\u8b77\u6027\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-3146","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-OK","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3146","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=3146"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3146\/revisions"}],"predecessor-version":[{"id":3147,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3146\/revisions\/3147"}],"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=3146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}