{"id":6519,"date":"2018-03-17T14:07:44","date_gmt":"2018-03-17T06:07:44","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6519"},"modified":"2018-03-17T14:08:36","modified_gmt":"2018-03-17T06:08:36","slug":"%e7%90%86%e8%a7%a3-python-%e4%b8%ad%e7%9a%84-super","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6519.html","title":{"rendered":"\u7406\u89e3 Python \u4e2d\u7684 super"},"content":{"rendered":"<p>\u8981\u8c08\u8bba super\uff0c\u9996\u5148\u6211\u4eec\u5e94\u8be5\u65e0\u89c6 &#8220;super&#8221; \u8fd9\u4e2a\u540d\u5b57\u5e26\u7ed9\u6211\u4eec\u7684\u5e72\u6270\u3002<\/p>\n<p>\u4e0d\u8981\u4e00\u8bf4\u5230 super \u5c31\u60f3\u5230\u7236\u7c7b\uff01super \u6307\u7684\u662f MRO \u4e2d\u7684\u4e0b\u4e00\u4e2a\u7c7b\uff01<br \/>\n\u4e0d\u8981\u4e00\u8bf4\u5230 super \u5c31\u60f3\u5230\u7236\u7c7b\uff01super \u6307\u7684\u662f MRO \u4e2d\u7684\u4e0b\u4e00\u4e2a\u7c7b\uff01<br \/>\n\u4e0d\u8981\u4e00\u8bf4\u5230 super \u5c31\u60f3\u5230\u7236\u7c7b\uff01super \u6307\u7684\u662f MRO \u4e2d\u7684\u4e0b\u4e00\u4e2a\u7c7b\uff01<\/p>\n<p>\u4e00\u8bf4\u5230 super \u5c31\u60f3\u5230\u7236\u7c7b\u8fd9\u662f\u521d\u5b66\u8005\u5f88\u5bb9\u6613\u72af\u7684\u4e00\u4e2a\u9519\u8bef\uff0c\u4e5f\u662f\u6211\u5f53\u5e74\u72af\u7684\u9519\u8bef\u3002 \u5fd8\u8bb0\u4e86\u8fd9\u4ef6\u4e8b\u4e4b\u540e\uff0c\u518d\u53bb\u770b\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/rhettinger.wordpress.com\/2011\/05\/26\/super-considered-super\/\">Python\u2019s super() considered super! <\/a>\u8fd9\u662f Raymond Hettinger \u5199\u7684\u4e00\u7bc7\u6587\u7ae0\uff0c\u4e5f\u662f\u5168\u4e16\u754c\u516c\u8ba4\u7684\u5bf9 super \u8bb2\u89e3\u6700\u900f\u5f7b\u7684\u4e00\u7bc7\u6587\u7ae0\uff0c\u51e1\u662f\u8ba8\u8bba super \u90fd\u4e00\u5b9a\u4f1a\u63d0\u5230\u5b83\uff08\u5f53\u7136\u8fd8\u6709\u4e00\u7bc7 Python&#8217;s Super Considered Harmful\uff09\u3002<\/p>\n<p>\u5982\u679c\u4e0d\u60f3\u770b\u957f\u7bc7\u5927\u8bba\u5c31\u53bb\u770b<a href=\"http:\/\/stackoverflow.com\/questions\/15896265\/python-super-inheritance-and-arguments-needed\/15896594#15896594\">\u8fd9\u4e2a\u7b54\u6848<\/a>\uff0csuper \u5176\u5b9e\u5e72\u7684\u662f\u8fd9\u4ef6\u4e8b\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef super(cls, inst):\r\n    mro = inst.__class__.mro()\r\n    return mro&#x5B;mro.index(cls) + 1]\r\n<\/pre>\n<p>\u4e24\u4e2a\u53c2\u6570 cls \u548c inst \u5206\u522b\u505a\u4e86\u4e24\u4ef6\u4e8b\uff1a<\/p>\n<ol>\n<li>inst \u8d1f\u8d23\u751f\u6210 MRO \u7684 list<\/li>\n<li>\u901a\u8fc7 cls \u5b9a\u4f4d\u5f53\u524d MRO \u4e2d\u7684 index, \u5e76\u8fd4\u56de mro[index + 1]<\/li>\n<\/ol>\n<p>\u8fd9\u4e24\u4ef6\u4e8b\u624d\u662f super \u7684\u5b9e\u8d28\uff0c\u4e00\u5b9a\u8981\u8bb0\u4f4f\uff01<\/p>\n<p>MRO \u5168\u79f0 Method Resolution Order\uff0c\u5b83\u4ee3\u8868\u4e86\u7c7b\u7ee7\u627f\u7684\u987a\u5e8f\u3002\u540e\u9762\u8be6\u7ec6\u8bf4\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass Root(object):\r\n    def __init__(self):\r\n        print(&quot;this is Root&quot;)\r\n\r\nclass B(Root):\r\n    def __init__(self):\r\n        print(&quot;enter B&quot;)\r\n        # print(self)  # this will print &lt;__main__.D object at 0x...&gt;\r\n        super(B, self).__init__()\r\n        print(&quot;leave B&quot;)\r\n\r\nclass C(Root):\r\n    def __init__(self):\r\n        print(&quot;enter C&quot;)\r\n        super(C, self).__init__()\r\n        print(&quot;leave C&quot;)\r\n\r\nclass D(B, C):\r\n    pass\r\n\r\nd = D()\r\nprint(d.__class__.__mro__)\r\n<\/pre>\n<p>\u8f93\u51fa<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nenter B\r\nenter C\r\nthis is Root\r\nleave C\r\nleave B\r\n(&lt;class '__main__.D'&gt;, &lt;class '__main__.B'&gt;, &lt;class '__main__.C'&gt;, &lt;class '__main__.Root'&gt;, &lt;type 'object'&gt;)\r\n<\/pre>\n<p>\u77e5\u9053\u4e86 super \u548c\u7236\u7c7b\u5176\u5b9e\u6ca1\u6709\u5b9e\u8d28\u5173\u8054\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u4e0d\u96be\u7406\u89e3\u4e3a\u4ec0\u4e48 enter B \u4e0b\u4e00\u53e5\u662f enter C \u800c\u4e0d\u662f this is Root\uff08\u5982\u679c\u8ba4\u4e3a super \u4ee3\u8868\u201c\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u201d\uff0c\u4f1a\u60f3\u5f53\u7136\u7684\u8ba4\u4e3a\u4e0b\u4e00\u53e5\u5e94\u8be5\u662fthis is Root\uff09\u3002\u6d41\u7a0b\u5982\u4e0b\uff0c\u5728 B \u7684 __init__ \u51fd\u6570\u4e2d\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nsuper(B, self).__init__()\r\n<\/pre>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u83b7\u53d6 self.__class__.__mro__\uff0c\u6ce8\u610f\u8fd9\u91cc\u7684 self \u662f D \u7684 instance \u800c\u4e0d\u662f B \u7684<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n(&lt;class '__main__.D'&gt;, &lt;class '__main__.B'&gt;, &lt;class '__main__.C'&gt;, &lt;class '__main__.Root'&gt;, &lt;type 'object'&gt;)\r\n<\/pre>\n<p>\u7136\u540e\uff0c\u901a\u8fc7 B \u6765\u5b9a\u4f4d MRO \u4e2d\u7684 index\uff0c\u5e76\u627e\u5230\u4e0b\u4e00\u4e2a\u3002\u663e\u7136 B \u7684\u4e0b\u4e00\u4e2a\u662f C\u3002\u4e8e\u662f\uff0c\u6211\u4eec\u8c03\u7528 C \u7684 __init__\uff0c\u6253\u51fa enter C\u3002<\/p>\n<p>\u987a\u4fbf\u8bf4\u4e00\u53e5\u4e3a\u4ec0\u4e48 B \u7684 __init__ \u4f1a\u88ab\u8c03\u7528\uff1a\u56e0\u4e3a D \u6ca1\u6709\u5b9a\u4e49 __init__\uff0c\u6240\u4ee5\u4f1a\u5728 MRO \u4e2d\u627e\u4e0b\u4e00\u4e2a\u7c7b\uff0c\u53bb\u67e5\u770b\u5b83\u6709\u6ca1\u6709\u5b9a\u4e49 __init__\uff0c\u4e5f\u5c31\u662f\u53bb\u8c03\u7528 B \u7684 __init__\u3002<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e00\u5207\u903b\u8f91\u8fd8\u662f\u5f88\u6e05\u6670\u7684\uff0c\u5173\u952e\u662f\u7406\u89e3 super \u5230\u5e95\u505a\u4e86\u4ec0\u4e48\u3002<\/p>\n<p>\u4e8e\u662f\uff0cMRO \u4e2d\u7c7b\u7684\u987a\u5e8f\u5230\u5e95\u662f\u600e\u4e48\u6392\u7684\u5462\uff1fPython\u2019s super() considered super!\u4e2d\u5df2\u7ecf\u6709\u5f88\u597d\u7684\u89e3\u91ca\uff0c\u6211\u7ffb\u8bd1\u4e00\u4e0b\uff1a<\/p>\n<ul>\n<li>\u5728 MRO \u4e2d\uff0c\u57fa\u7c7b\u6c38\u8fdc\u51fa\u73b0\u5728\u6d3e\u751f\u7c7b\u540e\u9762<\/li>\n<li>\u5982\u679c\u6709\u591a\u4e2a\u57fa\u7c7b\uff0c\u57fa\u7c7b\u7684\u76f8\u5bf9\u987a\u5e8f\u4fdd\u6301\u4e0d\u53d8<\/li>\n<\/ul>\n<p>\u5173\u4e8e MRO \u7684\u5b98\u65b9\u6587\u6863\u53c2\u89c1\uff1a<a href=\"https:\/\/www.python.org\/download\/releases\/2.3\/mro\/\">The Python 2.3 Method Resolution Order<\/a>\uff0c\u6709\u4e00\u4e9b\u5173\u4e8e MRO \u987a\u5e8f\u7684\u7406\u8bba\u4e0a\u7684\u89e3\u91ca\u3002<\/p>\n<p>\u6700\u540e\u7684\u6700\u540e\uff0c\u63d0\u9192\u5927\u5bb6. \u4ec0\u4e48 super \u554a\uff0cMRO \u554a\uff0c\u90fd\u662f\u9488\u5bf9 new-style class\u3002\u5982\u679c\u4e0d\u662f new-style class\uff0c\u5c31\u8001\u8001\u5b9e\u5b9e\u7528\u7236\u7c7b\u7684\u7c7b\u540d\u53bb\u8c03\u7528\u51fd\u6570\u5427\u3002<\/p>\n<p>\u8f6c\u81ea https:\/\/laike9m.com\/blog\/li-jie-python-super,70\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u8c08\u8bba super\uff0c\u9996\u5148\u6211\u4eec\u5e94\u8be5\u65e0\u89c6 &#8220;super&#8221; \u8fd9\u4e2a\u540d\u5b57\u5e26\u7ed9\u6211\u4eec\u7684\u5e72\u6270\u3002 \u4e0d\u8981\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-6519","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6519"}],"version-history":[{"count":2,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6519\/revisions"}],"predecessor-version":[{"id":6521,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6519\/revisions\/6521"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}