{"id":3532,"date":"2025-06-03T14:36:24","date_gmt":"2025-06-04T07:19:23","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3532"},"modified":"2025-06-04T14:36:24","modified_gmt":"2025-06-04T07:19:23","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84super%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%ad%e7%9a%84super%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 \u4e2d\u7684 `super()` \u51fd\u6578\uff1a\u6700\u65b0\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u66f4\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, super(), Python \u7a0b\u5f0f\u8a2d\u8a08, \u591a\u91cd\u7e7c\u627f, OOP\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 <code>super()<\/code> \u51fd\u6578<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`super()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u8b93\u6211\u5011\u5728\u5b50\u985e\u5225\u4e2d\u7e7c\u627f\u7236\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u800c\u7121\u9700\u660e\u78ba\u6307\u5b9a\u7236\u985e\u5225\u7684\u540d\u7a31\u3002\u9019\u4f7f\u5f97\u4ee3\u78bc\u66f4\u5177\u53ef\u8b80\u6027\uff0c\u4e5f\u7c21\u5316\u4e86\u591a\u91cd\u7e7c\u627f\u7684\u904e\u7a0b\u3002\u96a8\u8457 Python \u8a9e\u8a00\u7684\u6f14\u9032\uff0c`super()` \u7684\u4f7f\u7528\u65b9\u5f0f\u4e5f\u5f97\u5230\u4e86\u6539\u9032\uff0c\u8b93\u6211\u5011\u4f86\u770b\u770b 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`super()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\u662f\u7528\u4f86\u547c\u53eb\u7236\u985e\u5225\u7684\u65b9\u6cd5\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\nclass Parent:\n    def __init__(self):\n        print(\"Calling parent constructor\")\n\nclass Child(Parent):\n    def __init__(self):\n        print(\"Calling child constructor\")\n        super().__init__()\n\nc = Child()\n<\/pre>\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\uff0c`Child` \u985e\u5225\u7e7c\u627f\u81ea `Parent` \u985e\u5225\u3002\u5728 `Child` \u985e\u5225\u7684\u69cb\u9020\u51fd\u5f0f\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `super()` \u4f86\u547c\u53eb `Parent` \u985e\u5225\u7684\u69cb\u9020\u51fd\u5f0f\u3002\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>\nCalling child constructor\nCalling parent constructor\n<\/pre>\n<p>\u9019\u5c55\u793a\u4e86 `super()` \u5982\u4f55\u8b93\u6211\u5011\u5728\u5b50\u985e\u5225\u4e2d\u547c\u53eb\u7236\u985e\u5225\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u9700\u8981\u660e\u78ba\u6307\u5b9a\u7236\u985e\u5225\u7684\u540d\u7a31\u3002<\/p>\n<h2>\u9032\u968e\u7528\u6cd5\uff1a\u7372\u53d6\u7236\u985e\u5225\u7684\u5c6c\u6027<\/h2>\n<p>\u9664\u4e86\u547c\u53eb\u7236\u985e\u5225\u7684\u65b9\u6cd5\uff0c`super()` \u9084\u80fd\u7528\u4f86\u7372\u53d6\u7236\u985e\u5225\u7684\u5c6c\u6027\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python\">\nclass Parent:\n    def __init__(self):\n        self.value = \"foo\"\n\nclass Child(Parent):\n    def __init__(self):\n        super().__init__()\n        print(self.value)\n\nc = Child()\n<\/pre>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c`Child` \u985e\u5225\u5728\u5176\u69cb\u9020\u51fd\u5f0f\u4e2d\u4f7f\u7528 `super()` \u4f86\u521d\u59cb\u5316 `Parent` \u985e\u5225\uff0c\u4e26\u8f38\u51fa `value` \u5c6c\u6027\u3002\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<pre>\nfoo\n<\/pre>\n<p>\u9019\u986f\u793a\u4e86 `super()` \u5982\u4f55\u8b93\u6211\u5011\u5728\u5b50\u985e\u5225\u4e2d\u7372\u53d6\u7236\u985e\u5225\u7684\u5c6c\u6027\uff0c\u7121\u9700\u660e\u78ba\u6307\u5b9a\u7236\u985e\u5225\u7684\u540d\u7a31\u3002<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u53ca\u6392\u9664<\/h2>\n<p>\u4f7f\u7528 `super()` \u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<br \/>\n1. **\u672a\u6b63\u78ba\u4f7f\u7528 `super()`**\uff1a\u5728 Python 2 \u4e2d\uff0c\u5fc5\u9808\u50b3\u905e\u985e\u5225\u548c\u5be6\u4f8b\uff0cPython 3 \u4e2d\u5247\u4e0d\u9700\u8981\u3002<br \/>\n2. **\u591a\u91cd\u7e7c\u627f\u4e2d\u7684\u6df7\u6dc6**\uff1a\u5728\u591a\u91cd\u7e7c\u627f\u4e2d\uff0c\u78ba\u4fdd\u4f7f\u7528 `super()` \u6642\u7684\u547c\u53eb\u9806\u5e8f\u6b63\u78ba\uff0c\u5efa\u8b70\u67e5\u95b1 [Python \u7684\u65b9\u6cd5\u89e3\u6790\u9806\u5e8f\uff08MRO\uff09](https:\/\/vocus.cc\/article\/6489c2e4f2e3b10001b69d3b) \u4ee5\u4e86\u89e3\u66f4\u591a\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>`super()` \u51fd\u6578\u7684\u61c9\u7528\u4e0d\u50c5\u9650\u65bc\u69cb\u9020\u51fd\u5f0f\uff0c\u9084\u53ef\u4ee5\u5728\u5176\u4ed6\u65b9\u6cd5\u4e2d\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: python\">\nclass Base:\n    def show(self):\n        print(\"Base class show\")\n\nclass Derived(Base):\n    def show(self):\n        super().show()\n        print(\"Derived class show\")\n\nd = Derived()\nd.show()\n<\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c55\u793a\u4e86\u5982\u4f55\u5728\u65b9\u6cd5\u4e2d\u4f7f\u7528 `super()` \u4f86\u64f4\u5c55\u7236\u985e\u5225\u7684\u529f\u80fd\u3002\u57f7\u884c\u7d50\u679c\u70ba\uff1a<\/p>\n<pre>\nBase class show\nDerived class show\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86 Python \u4e2d\u7684 `super()` \u51fd\u6578\u7684\u6700\u65b0\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u8b93\u6211\u5011\u80fd\u5920\u65b9\u4fbf\u5730\u5728\u5b50\u985e\u5225\u4e2d\u7e7c\u627f\u7236\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\u3002\u6b63\u78ba\u4f7f\u7528 `super()` \u4e0d\u50c5\u80fd\u7c21\u5316\u4ee3\u78bc\uff0c\u9084\u80fd\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `super()` \u548c\u76f4\u63a5\u547c\u53eb\u7236\u985e\u5225\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: \u4f7f\u7528 `super()` \u53ef\u4ee5\u907f\u514d\u786c\u7de8\u78bc\u7236\u985e\u5225\u7684\u540d\u7a31\uff0c\u4e26\u5728\u591a\u91cd\u7e7c\u627f\u6642\u78ba\u4fdd\u6b63\u78ba\u7684\u547c\u53eb\u9806\u5e8f\u3002<\/p>\n<p>**Q2: \u5728 Python 2 \u548c Python 3 \u4e2d\uff0c`super()` \u7684\u7528\u6cd5\u6709\u4ec0\u9ebc\u5dee\u7570\uff1f**<br \/>\nA2: Python 2 \u9700\u8981\u63d0\u4f9b\u985e\u5225\u548c\u5be6\u4f8b\u4f5c\u70ba\u53c3\u6578\uff0c\u800c Python 3 \u5247\u4e0d\u9700\u8981\u9019\u6a23\u505a\u3002<\/p>\n<p>**Q3: \u6211\u53ef\u4ee5\u5728\u975c\u614b\u65b9\u6cd5\u4e2d\u4f7f\u7528 `super()` \u55ce\uff1f**<br \/>\nA3: \u4e0d\u53ef\u4ee5\uff0c\u56e0\u70ba\u975c\u614b\u65b9\u6cd5\u4e0d\u5177\u5099\u5c0d\u8c61\u4e0a\u4e0b\u6587\uff0c`super()` \u9700\u8981\u5be6\u4f8b\u4e0a\u4e0b\u6587\u4f86\u904b\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d Python \u7684\u5176\u4ed6\u9ad8\u7d1a\u529f\u80fd\u611f\u8208\u8da3\uff0c\u53ef\u4ee5\u53c3\u8003\u6211\u5011\u7684 [Python \u591a\u91cd\u7e7c\u627f\u6559\u5b78](https:\/\/vocus.cc\/article\/6489c2e4f2e3b10001b69d3b)\uff0c\u6df1\u5165\u4e86\u89e3\u66f4\u591a\u5be6\u4f5c\u6280\u5de7\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684super()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u5b50\u985e\u5225\u4e2d\u8a2a\u554f\u7236\u985e\u5225\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u4e26\u70ba\u4f60\u7684\u7a0b\u5f0f\u78bc\u63d0\u4f9b\u66f4\u591a\u7684\u5f48\u6027\u548c\u53ef\u64f4\u5c55\u6027\uff0c\u8b93\u4f60\u7684\u7a0b\u5f0f\u78bc\u66f4\u52a0\u512a\u96c5\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-3532","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-UY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3532","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=3532"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3532\/revisions"}],"predecessor-version":[{"id":3533,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3532\/revisions\/3533"}],"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=3532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}