{"id":2888,"date":"2025-05-29T16:49:08","date_gmt":"2025-06-04T09:29:08","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2888"},"modified":"2025-06-04T16:49:08","modified_gmt":"2025-06-04T09:29:08","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84super%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84super%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 super() \u51fd\u6578\uff1a2025 \u6700\u65b0\u6559\u5b78\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, super(), Python \u6559\u5b78, Python \u7e7c\u627f\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 super() \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`super()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5920\u5e6b\u52a9\u6211\u5011\u65b9\u4fbf\u5730\u8a2a\u554f\u7236\u985e\u4e2d\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u9019\u4e0d\u50c5\u53ef\u4ee5\u4f7f\u7a0b\u5f0f\u78bc\u66f4\u5177\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u907f\u514d\u786c\u7de8\u78bc\u7236\u985e\u540d\u7a31\uff0c\u63d0\u5347\u7a0b\u5f0f\u7684\u9748\u6d3b\u6027\u3002<\/p>\n<p>## \u70ba\u4ec0\u9ebc\u4f7f\u7528 super()\uff1f<\/p>\n<p>\u4f7f\u7528 `super()` \u51fd\u6578\u53ef\u4ee5\u8b93\u6211\u5011\u5728\u5b50\u985e\u4e2d\u7e7c\u627f\u7236\u985e\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u660e\u78ba\u6307\u5b9a\u7236\u985e\u7684\u540d\u7a31\u3002\u9019\u6a23\u7684\u8a2d\u8a08\u4f7f\u5f97\u5728\u591a\u91cd\u7e7c\u627f\u7684\u60c5\u6cc1\u4e0b\uff0cPython \u80fd\u5920\u81ea\u52d5\u6c7a\u5b9a\u547c\u53eb\u54ea\u500b\u7236\u985e\u7684\u65b9\u6cd5\uff0c\u9019\u5c0d\u65bc\u7dad\u8b77\u548c\u64f4\u5c55\u7a0b\u5f0f\u78bc\u7279\u5225\u91cd\u8981\u3002<\/p>\n<p>## 2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 `super()` \u51fd\u6578\u7684\u7bc4\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728\u5b50\u985e\u4e2d\u8abf\u7528\u7236\u985e\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Parent:<br \/>\n    def __init__(self):<br \/>\n        self.name = &#8216;Parent&#8217;<br \/>\n        print(f&#8221;{self.name} initialized.&#8221;)<\/p>\n<p>class Child(Parent):<br \/>\n    def __init__(self):<br \/>\n        super().__init__()  # \u8abf\u7528\u7236\u985e\u7684\u521d\u59cb\u5316\u65b9\u6cd5<br \/>\n        self.name = &#8216;Child&#8217;<br \/>\n        print(f&#8221;{self.name} initialized.&#8221;)<\/p>\n<p>child = Child()<br \/>\nprint(child.name)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u7576\u5275\u5efa `Child` \u985e\u7684\u5be6\u4f8b\u6642\uff0c\u6703\u9996\u5148\u8abf\u7528 `Parent` \u985e\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u7136\u5f8c\u518d\u9032\u884c `Child` \u985e\u7684\u5c6c\u6027\u8a2d\u5b9a\u3002\u9019\u6a23\uff0c\u6211\u5011\u53ef\u4ee5\u78ba\u4fdd\u7236\u985e\u7684\u521d\u59cb\u5316\u908f\u8f2f\u88ab\u6b63\u78ba\u57f7\u884c\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `super()` \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n&#8211; \u5fd8\u8a18\u5728\u5b50\u985e\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u4e2d\u8abf\u7528 `super().__init__()`\uff0c\u5c0e\u81f4\u7236\u985e\u7684\u5c6c\u6027\u672a\u88ab\u6b63\u78ba\u521d\u59cb\u5316\u3002<br \/>\n&#8211; \u5728\u591a\u91cd\u7e7c\u627f\u4e2d\uff0c\u672a\u6b63\u78ba\u7406\u89e3\u65b9\u6cd5\u89e3\u6790\u9806\u5e8f\uff08Method Resolution Order, MRO\uff09\uff0c\u53ef\u80fd\u5c0e\u81f4\u610f\u5916\u7684\u884c\u70ba\u3002<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u6aa2\u67e5\u4e00\u500b\u985e\u7684 MRO\uff1a<\/p>\n<p>&#8220;`python<br \/>\nprint(Child.__mro__)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u5c07\u986f\u793a Python \u5728\u5c0b\u627e\u65b9\u6cd5\u6642\u7684\u985e\u5225\u9806\u5e8f\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>`super()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u521d\u59cb\u5316\u65b9\u6cd5\u7684\u547c\u53eb\uff0c\u5b83\u4e5f\u53ef\u7528\u65bc\u8abf\u7528\u5176\u4ed6\u7236\u985e\u7684\u65b9\u6cd5\u3002\u9019\u5c0d\u65bc\u9700\u8981\u64f4\u5c55\u6216\u4fee\u6539\u7236\u985e\u884c\u70ba\u7684\u60c5\u6cc1\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Parent:<br \/>\n    def greet(self):<br \/>\n        return &#8220;Hello from Parent&#8221;<\/p>\n<p>class Child(Parent):<br \/>\n    def greet(self):<br \/>\n        parent_greeting = super().greet()<br \/>\n        return f&#8221;{parent_greeting} and Child&#8221;<\/p>\n<p>child = Child()<br \/>\nprint(child.greet())<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`Child` \u985e\u8986\u5beb\u4e86 `greet` \u65b9\u6cd5\uff0c\u4f46\u4ecd\u7136\u4f7f\u7528\u4e86\u7236\u985e\u7684 `greet` \u65b9\u6cd5\uff0c\u9019\u6a23\u53ef\u4ee5\u8f15\u9b06\u64f4\u5c55\u7236\u985e\u7684\u884c\u70ba\u3002<\/p>\n<p>## \u7e3d\u7d50<\/p>\n<p>`super()` \u51fd\u6578\u662f Python \u4e2d\u4e00\u500b\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\uff0c\u7279\u5225\u662f\u5728\u9762\u5c0d\u7269\u4ef6\u5c0e\u5411\u7de8\u7a0b\u7684\u7e7c\u627f\u8207\u591a\u91cd\u7e7c\u627f\u6642\u3002\u4f7f\u7528 `super()` \u53ef\u4ee5\u63d0\u5347\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u8207\u53ef\u7dad\u8b77\u6027\uff0c\u4e26\u4e14\u80fd\u5920\u6709\u6548\u907f\u514d\u8a31\u591a\u6f5b\u5728\u7684\u932f\u8aa4\u3002<\/p>\n<p>\u82e5\u60f3\u4e86\u89e3\u66f4\u591a Python \u7684\u4f7f\u7528\u6280\u5de7\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/631c0e9bfd89780001f98c32)\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `super()` \u8207\u76f4\u63a5\u547c\u53eb\u7236\u985e\u65b9\u6cd5\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: \u4f7f\u7528 `super()` \u53ef\u4ee5\u81ea\u52d5\u7372\u53d6\u6b63\u78ba\u7684\u7236\u985e\u65b9\u6cd5\uff0c\u7279\u5225\u662f\u5728\u591a\u91cd\u7e7c\u627f\u6642\uff0c\u80fd\u907f\u514d\u786c\u7de8\u78bc\u7236\u985e\u540d\u7a31\uff0c\u63d0\u5347\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<p>**Q2: \u5728\u4ec0\u9ebc\u60c5\u6cc1\u4e0b\u61c9\u8a72\u4f7f\u7528 `super()`\uff1f**<br \/>\nA2: \u7576\u4f60\u9700\u8981\u5728\u5b50\u985e\u4e2d\u91cd\u7528\u7236\u985e\u7684\u5c6c\u6027\u6216\u65b9\u6cd5\uff0c\u6216\u662f\u7576\u4f60\u4f7f\u7528\u591a\u91cd\u7e7c\u627f\u6642\uff0c\u4f7f\u7528 `super()` \u662f\u6700\u4f73\u9078\u64c7\u3002<\/p>\n<p>**Q3: \u5982\u679c\u5fd8\u8a18\u8abf\u7528 `super()` \u6703\u767c\u751f\u4ec0\u9ebc\u60c5\u6cc1\uff1f**<br \/>\nA3: \u5982\u679c\u672a\u8abf\u7528 `super()`\uff0c\u7236\u985e\u7684\u521d\u59cb\u5316\u908f\u8f2f\u5c07\u4e0d\u6703\u57f7\u884c\uff0c\u53ef\u80fd\u5c0e\u81f4\u5c6c\u6027\u672a\u6b63\u78ba\u8a2d\u7f6e\uff0c\u5f9e\u800c\u5f15\u767c\u932f\u8aa4\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Super()\u51fd\u6578\u662fPython\u4e2d\u7684\u4e00\u500b\u91cd\u8981\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u8a2a\u554f\u7236\u985e\u4e2d\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u8b93\u4f60\u66f4\u597d\u5730\u7406\u89e3Python\u7684\u9762\u5411\u5c0d\u8c61\u7a0b\u5f0f\u8a2d\u8a08\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Super()\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5728Python\u4e2d\u7684\u61c9\u7528\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-2888","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-KA","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2888","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=2888"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2888\/revisions"}],"predecessor-version":[{"id":13015,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2888\/revisions\/13015"}],"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=2888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}