{"id":138199,"date":"2020-07-21T06:49:32","date_gmt":"2020-07-20T22:49:32","guid":{"rendered":"http:\/\/4563.org\/?p=138199"},"modified":"2020-07-21T06:49:32","modified_gmt":"2020-07-20T22:49:32","slug":"python-%e5%a4%9a%e9%87%8d%e7%bb%a7%e6%89%bf%e7%9a%84%e9%97%ae%e9%a2%98-%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bb%a5%e4%b8%8b%e4%bb%a3%e7%a0%81%e9%87%8c-base-%e7%9a%84-__init__-%e6%b2%a1%e6%9c%89%e8%a2%ab","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=138199","title":{"rendered":"Python \u591a\u91cd\u7ee7\u627f\u7684\u95ee\u9898 \u4e3a\u4ec0\u4e48\u4ee5\u4e0b\u4ee3\u7801\u91cc Base \u7684 __init__ \u6ca1\u6709\u88ab\u8c03\u7528"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  Python \u591a\u91cd\u7ee7\u627f\u7684\u95ee\u9898 \u4e3a\u4ec0\u4e48\u4ee5\u4e0b\u4ee3\u7801\u91cc Base \u7684 __init__ \u6ca1\u6709\u88ab\u8c03\u7528               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : doraemon1293 <\/span>  <span><i><\/i> 8<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>class Base():     def __init__(self):         print(\"Base\")  class First(Base):     def __init__(self):         super().__init__()         print(\"first\")  class Second(Base):     def __init__(self):         print(\"second\")  class Third(First,Second):     def __init__(self):         super().__init__()         print(\"third\") Third() <\/code><\/pre>\n<p>\u4e3a\u4f55\u8f93\u51fa\u7ed3\u679c\u4e3a<\/p>\n<p>second<\/p>\n<p>first<\/p>\n<p>third<\/p>\n<p>\u4e3a\u4ec0\u4e48 First \u4e2d super().<strong>init<\/strong>() \u6ca1\u6709\u88ab\u8c03\u7528\u5462<\/p>\n<p>\u5982\u679c\u4ee3\u7801\u6539\u4e3a<\/p>\n<pre><code>class Base():     def __init__(self):         print(\"Base\")  class First(Base):     def __init__(self):         super().__init__()         print(\"first\")  class Second(Base):     def __init__(self):         super().__init__()         print(\"second\")  class Third(First,Second):     def __init__(self):         super().__init__()         print(\"third\") Third() <\/code><\/pre>\n<p>\u5219\u8f93\u51fa\u7ed3\u679c\u4e3a<\/p>\n<p>Base<\/p>\n<p>second<\/p>\n<p>first<\/p>\n<p>third<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2572653\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gwy15 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8c03\u7528 super() \u7684\u65f6\u5019\u5b9e\u9645\u4e0a\u4f1a\u8ddf\u968f MRO \u8fdb\u884c\u94fe\u5f0f\u8c03\u7528\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc\u9762\uff0c\u8c03\u7528\u94fe\u662f\uff08\u4ece\u5de6\u5230\u53f3\u5bbd\u5ea6\u4f18\u5148\u641c\u7d22\uff09<br \/>`Third` -&gt; `First` -&gt; `Second` -&gt; `Base` -&gt; `object`<br \/>\u53ef\u4ee5\u7528 `Third.mro()` \u67e5\u770b\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u8c03\u7528\u7684\u65f6\u5019\uff0cThird.__init__ \u91cc\u9762 super().__init__() \u5b9e\u9645\u8c03\u7528\u7684\u662f Second.__init__(self)\uff0c\u800c Second.__init__ \u91cc\u9762\u6ca1\u6709\u7ee7\u7eed\u8c03\u7528 super\uff0c\u6240\u4ee5 Base \u7684 __init__ \u6ca1\u6709\u8c03\u7528\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572654\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gwy15 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a\u9762\u6709\u70b9\u7b14\u8bef<br \/> Third -&gt; First -&gt; Second -\/-&gt; Base<br \/> &gt;&gt;&gt; second<br \/> &gt;&gt;&gt; first<br \/>&gt;&gt;&gt; third                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572655\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : marquina <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gwy15 \u66f4\u51c6\u786e\u5730\u8bf4\uff0cThird.__init__\u91cc\u7684 super().__init__()\u8c03\u7528\u7684\u662f First.__init__\uff0cFirst.__init__\u91cc\u7684 super().__init__\u8c03\u7528\u7684\u662f Second.__init__\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572656\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : marquina <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gwy15 \u624d\u770b\u5230\u4f60#2 \u7684\u56de\u590d                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572657\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : doraemon1293 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u8c22\u56de\u7b54<br \/>\u4e24\u79cd\u60c5\u51b5 \u6253\u5370 Third.__mro__ \u7ed3\u679c\u90fd\u662f<br \/>(&lt;class &#8216;__main__.Third&#8217;&gt;, &lt;class &#8216;__main__.First&#8217;&gt;, &lt;class &#8216;__main__.Second&#8217;&gt;, &lt;class &#8216;__main__.Base&#8217;&gt;, &lt;class &#8216;object&#8217;&gt;)<br \/>\u591a\u91cd\u7ee7\u627f super \u4e0d\u662f\u4ece\u5de6\u5230\u53f3\u67e5\u627e\u5417 \u4e3a\u4ec0\u4e48\u4f1a\u8c03\u7528 Second.__init__(self)<\/p>\n<p>\u6211\u628a\u6253\u5370\u7684\u4ee3\u7801\u6539\u6210\u6807\u6ce8\u5f00\u59cb\u548c\u7ed3\u675f<br \/>&#8220;`<br \/>class Base():<br \/> def __init__(self):<br \/> print(&#8220;Base&#8221;)<\/p>\n<p>class First(Base):<br \/> def __init__(self):<br \/> print(&#8220;first start&#8221;)<br \/> super().__init__()<br \/> print(&#8220;first end&#8221;)<br \/>class Second(Base):<br \/> def __init__(self):<br \/> print(&#8220;second start&#8221;)<br \/> # super().__init__()<br \/> print(&#8220;second end&#8221;)<\/p>\n<p>class Third(First,Second):<br \/> def __init__(self):<br \/> super().__init__()<br \/> print(&#8220;third&#8221;)<br \/>print(Third.__mro__)<br \/>Third()<br \/>&#8220;`<br \/>\u7ed3\u679c\u5982\u4e0b<br \/>first start<br \/>second start<br \/>second end<br \/>first end<br \/>third<br \/>\u4e3a\u4ec0\u4e48 first \u5148\u88ab\u8c03\u7528 \u7136\u800c first \u91cc\u7684 super().__init__()\u6ca1\u6709\u4efb\u4f55\u6548\u679c\u5462?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572658\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : doraemon1293 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u521a\u770b\u5230\u4f60\u7684\u66f4\u6b63 \u8bf7\u5ffd\u7565\u6211\u4e0a\u9762\u7684\u56de\u590d<br \/>\u4e3a\u4ec0\u4e48 First.__init__\u91cc\u7684 super().__init__\u8c03\u7528\u7684\u662f Second.__init__\u3002<br \/>First \u7684 super \u4e0d\u5e94\u8be5\u8c03\u7528\u4ed6\u7684\u7236\u7c7b Base \u5417?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572659\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gwy15 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @doraemon1293 super \u662f\u6839\u636e MRO \u94fe\u8d70\u7684\uff0c\u4e0d\u662f\u201c\u7236\u7c7b\u201d\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572660\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : doraemon1293 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gwy15 <br \/>\u660e\u767d\u4e86 \u4e00\u76f4\u4ee5\u4e3a super \u662f\u8c03\u7528\u7236\u7c7b \u591a\u8c22\u89e3\u7b54                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572661\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : oahebky <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u662f\u4e2a\u65b0\u5f0f\u7c7b+\u94bb\u77f3\u7ee7\u627f\u7684\u95ee\u9898\u3002<\/p>\n<p>\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u7684\u4e1c\u897f\uff0c\u5c31\u662f\u201c\u89c4\u5b9a\u201d\u3002<\/p>\n<p>\u5177\u4f53\u53c2\u89c1 \u300a learning Python \u300b\uff08\u6709\u4e2d\u6587\u7248\uff09\u7c7b\u7684\u9ad8\u7ea7\u4e3b\u9898\u90e8\u5206 &#8211; \u7b2c 31 \u7ae0\u3002<\/p>\n<p>\u81ea\u8ba4\u4e3a\u6ca1\u6709\u80fd\u529b\u89e3\u91ca\u5f97\u6bd4\u4e66\u4e0a\u66f4\u597d\uff0c\u6240\u4ee5\u4e0d\u591a\u505a\u89e3\u91ca\uff0c\u4ec5\u6307\u8def\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2572662\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gulu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u5982\u4eb2\u81ea\u770b\u770b Python \u4e4b\u7236\u5f53\u65f6\u89e3\u51b3 MRO \u7684\u601d\u8def\uff1a<br \/>http:\/\/python-history.blogspot.com\/2010\/06\/method-resolution-order.html?m=1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>Python \u591a\u91cd\u7ee7\u627f\u7684\u95ee\u9898 \u4e3a\u4ec0&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/138199"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=138199"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/138199\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=138199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=138199"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=138199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}