{"id":221481,"date":"2021-07-11T09:57:48","date_gmt":"2021-07-11T01:57:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221481"},"modified":"2021-06-25T09:58:52","modified_gmt":"2021-06-25T01:58:52","slug":"usage-of-super","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/usage-of-super.html","title":{"rendered":"Python\u4e2d\u7684super()\u7528\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nPython\u4e2d\u7684super()\u65b9\u6cd5\u8bbe\u8ba1\u76ee\u7684\u662f\u7528\u6765\u89e3\u51b3\u591a\u91cd\u7ee7\u627f\u65f6\u7236\u7c7b\u7684\u67e5\u627e\u95ee\u9898\uff0c\u6240\u4ee5\u5728\u5355\u91cd\u7ee7\u627f\u4e2d\u7528\u4e0d\u7528 super \u90fd\u6ca1\u5173\u7cfb\uff1b\u4f46\u662f\uff0c\u4f7f\u7528 super() \u662f\u4e00\u4e2a\u597d\u7684\u4e60\u60ef\u3002\u4e00\u822c\u6211\u4eec\u5728\u5b50\u7c7b\u4e2d\u9700\u8981\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u65f6\u624d\u4f1a\u8fd9\u4e48\u7528\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u8bf4\u5230 super\uff0c \u5927\u5bb6\u53ef\u80fd\u89c9\u5f97\u5f88\u7b80\u5355\u5440\uff0c\u4e0d\u5c31\u662f\u7528\u6765\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5\u7684\u561b\u3002\u5982\u679c\u771f\u7684\u8fd9\u4e48\u7b80\u5355\u7684\u8bdd\u4e5f\u5c31\u4e0d\u4f1a\u6709\u8fd9\u7bc7\u6587\u7ae0\u4e86\uff0c\u4e14\u542c\u6211\u7ec6\u7ec6\u9053\u6765\u3002<\/p>\n

\u7ea6\u5b9a<\/strong><\/div>\n

\u5728\u5f00\u59cb\u4e4b\u524d\u6211\u4eec\u6765\u7ea6\u5b9a\u4e00\u4e0b\u672c\u6587\u6240\u4f7f\u7528\u7684 Python \u7248\u672c\u3002\u9ed8\u8ba4\u7528\u7684\u662f Python 3\uff0c\u4e5f\u5c31\u662f\u8bf4\uff1a\u672c\u6587\u6240\u5b9a\u4e49\u7684\u7c7b\u90fd\u662f\u65b0\u5f0f\u7c7b\u3002\u5982\u679c\u4f60\u7528\u5230\u662f Python 2 \u7684\u8bdd\uff0c\u8bb0\u5f97\u7ee7\u627f object:<\/p>\n

# \u9ed8\u8ba4\uff0c Python 3\r\nclass A:\r\n    pass\r\n\r\n# Python 2\r\nclass A(object):\r\n    pass<\/pre>\n

Python 3 \u548c Python 2 \u7684\u53e6\u4e00\u4e2a\u533a\u522b\u662f: Python 3 \u53ef\u4ee5\u4f7f\u7528\u76f4\u63a5\u4f7f\u7528 super().xxx \u4ee3\u66ff super(Class, self).xxx :<\/p>\n

# \u9ed8\u8ba4\uff0cPython 3\r\nclass B(A):\r\n    def add(self, x):\r\n        super().add(x)\r\n\r\n# Python 2\r\nclass B(A):\r\n    def add(self, x):\r\n        super(B, self).add(x)<\/pre>\n

\u6240\u4ee5\uff0c\u4f60\u5982\u679c\u7528\u7684\u662f Python 2 \u7684\u8bdd\uff0c\u8bb0\u5f97\u5c06\u672c\u6587\u7684 super() \u66ff\u6362\u4e3a suepr(Class, self) \u3002<\/p>\n

\u5982\u679c\u8fd8\u6709\u5176\u4ed6\u4e0d\u517c\u5bb9 Python 2 \u7684\u60c5\u51b5\uff0c\u6211\u4f1a\u5728\u6587\u4e2d\u6ce8\u660e\u7684\u3002<\/p>\n

\u5355\u7ee7\u627f<\/strong><\/div>\n

\u5728\u5355\u7ee7\u627f\u4e2d super \u5c31\u50cf\u5927\u5bb6\u6240\u60f3\u7684\u90a3\u6837\uff0c\u4e3b\u8981\u662f\u7528\u6765\u8c03\u7528\u7236\u7c7b\u7684\u65b9\u6cd5\u7684\u3002<\/p>\n

class A:\r\n    def __init__(self):\r\n        self.n = 2\r\n\r\n    def add(self, m):\r\n        print('self is {0} @A.add'.format(self))\r\n        self.n += m\r\n\r\n\r\nclass B(A):\r\n    def __init__(self):\r\n        self.n = 3\r\n\r\n    def add(self, m):\r\n        print('self is {0} @B.add'.format(self))\r\n        super().add(m)\r\n        self.n += 3<\/pre>\n

\u4f60\u89c9\u5f97\u6267\u884c\u4e0b\u9762\u4ee3\u7801\u540e\uff0c b.n \u7684\u503c\u662f\u591a\u5c11\u5462\uff1f<\/p>\n

b = B()\r\nb.add(2)\r\nprint(b.n)<\/pre>\n

\u6267\u884c\u7ed3\u679c\u5982\u4e0b:<\/p>\n

self is <__main__.B object at 0x106c49b38> @B.add\r\nself is <__main__.B object at 0x106c49b38> @A.add\r\n8<\/pre>\n

\u8fd9\u4e2a\u7ed3\u679c\u8bf4\u660e\u4e86\u4e24\u4e2a\u95ee\u9898:<\/p>\n

    \n
  1. super().add(m) \u786e\u5b9e\u8c03\u7528\u4e86\u7236\u7c7b A \u7684 add \u65b9\u6cd5\u3002<\/li>\n
  2. super().add(m) \u8c03\u7528\u7236\u7c7b\u65b9\u6cd5 def add(self, m) \u65f6, \u6b64\u65f6\u7236\u7c7b\u4e2d self \u5e76\u4e0d\u662f\u7236\u7c7b\u7684\u5b9e\u4f8b\u800c\u662f\u5b50\u7c7b\u7684\u5b9e\u4f8b, \u6240\u4ee5 b.add(2) \u4e4b\u540e\u7684\u7ed3\u679c\u662f 5 \u800c\u4e0d\u662f 4 \u3002<\/li>\n<\/ol>\n

    \u4e0d\u77e5\u9053\u8fd9\u4e2a\u7ed3\u679c\u662f\u5426\u548c\u4f60\u60f3\u5230\u4e00\u6837\u5462\uff1f\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u591a\u7ee7\u627f\u7684\u4f8b\u5b50\u3002<\/p>\n

    \u591a\u7ee7\u627f<\/strong><\/div>\n

    \u8fd9\u6b21\u6211\u4eec\u518d\u5b9a\u4e49\u4e00\u4e2a class C\uff0c\u4e00\u4e2a class D:<\/p>\n

    class C(A):\r\n    def __init__(self):\r\n        self.n = 4\r\n\r\n    def add(self, m):\r\n        print('self is {0} @C.add'.format(self))\r\n        super().add(m)\r\n        self.n += 4\r\n\r\n\r\nclass D(B, C):\r\n    def __init__(self):\r\n        self.n = 5\r\n\r\n    def add(self, m):\r\n        print('self is {0} @D.add'.format(self))\r\n        super().add(m)\r\n        self.n += 5<\/pre>\n

    \u4e0b\u9762\u7684\u4ee3\u7801\u53c8\u8f93\u51fa\u5565\u5462\uff1f<\/p>\n

    d = D()\r\nd.add(2)\r\nprint(d.n)<\/pre>\n

    \u8fd9\u6b21\u7684\u8f93\u51fa\u5982\u4e0b:<\/p>\n

    self is <__main__.D object at 0x10ce10e48> @D.add\r\nself is <__main__.D object at 0x10ce10e48> @B.add\r\nself is <__main__.D object at 0x10ce10e48> @C.add\r\nself is <__main__.D object at 0x10ce10e48> @A.add\r\n19<\/pre>\n

    \u4f60\u8bf4\u5bf9\u4e86\u5417\uff1f\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\u7c7b\u4f3c:<\/p>\n

    self is <__main__.D object at 0x10ce10e48> @D.add\r\nself is <__main__.D object at 0x10ce10e48> @B.add\r\nself is <__main__.D object at 0x10ce10e48> @A.add\r\n15<\/pre>\n

    \u4e3a\u4ec0\u4e48\u4f1a\u8ddf\u9884\u671f\u7684\u4e0d\u4e00\u6837\u5462\uff1f\u4e0b\u9762\u6211\u4eec\u5c06\u4e00\u8d77\u6765\u770b\u770b super \u7684\u5965\u79d8\u3002<\/p>\n

    super \u662f\u4e2a\u7c7b<\/strong><\/div>\n

    \u5f53\u6211\u4eec\u8c03\u7528 super() \u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a super \u7c7b\u3002\u4f60\u6ca1\u770b\u9519\uff0c super \u662f\u4e2a\u7c7b\uff0c\u65e2\u4e0d\u662f\u5173\u952e\u5b57\u4e5f\u4e0d\u662f\u51fd\u6570\u7b49\u5176\u4ed6\u6570\u636e\u7ed3\u6784:<\/p>\n

    >>> class A: pass\r\n...\r\n>>> s = super(A)\r\n>>> type(s)\r\n\r\n>>><\/class><\/pre>\n

    \u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c super \u5305\u542b\u4e86\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u4fe1\u606f: \u4e00\u4e2a MRO \u4ee5\u53ca MRO \u4e2d\u7684\u4e00\u4e2a\u7c7b\u3002\u5f53\u4ee5\u5982\u4e0b\u65b9\u5f0f\u8c03\u7528 super \u65f6:<\/p>\n

    super(a_type, obj)<\/pre>\n

    MRO \u6307\u7684\u662f type(obj) \u7684 MRO, MRO \u4e2d\u7684\u90a3\u4e2a\u7c7b\u5c31\u662f a_type , \u540c\u65f6 isinstance(obj, a_type) == True \u3002<\/p>\n

    \u5f53\u8fd9\u6837\u8c03\u7528\u65f6:<\/p>\n

    super(type1, type2)<\/pre>\n

    MRO \u6307\u7684\u662f type2 \u7684 MRO, MRO \u4e2d\u7684\u90a3\u4e2a\u7c7b\u5c31\u662f type1 \uff0c\u540c\u65f6 issubclass(type2, type1) == True \u3002<\/p>\n

    \u90a3\u4e48\uff0c super() \u5b9e\u9645\u4e0a\u505a\u4e86\u5565\u5462\uff1f\u7b80\u5355\u6765\u8bf4\u5c31\u662f\uff1a\u63d0\u4f9b\u4e00\u4e2a MRO \u4ee5\u53ca\u4e00\u4e2a MRO \u4e2d\u7684\u7c7b C \uff0c super() \u5c06\u8fd4\u56de\u4e00\u4e2a\u4ece MRO \u4e2d C \u4e4b\u540e\u7684\u7c7b\u4e2d\u67e5\u627e\u65b9\u6cd5\u7684\u5bf9\u8c61\u3002<\/p>\n

    \u4e5f\u5c31\u662f\u8bf4\uff0c\u67e5\u627e\u65b9\u5f0f\u65f6\u4e0d\u662f\u50cf\u5e38\u89c4\u65b9\u6cd5\u4e00\u6837\u4ece\u6240\u6709\u7684 MRO \u7c7b\u4e2d\u67e5\u627e\uff0c\u800c\u662f\u4ece MRO \u7684 tail \u4e2d\u67e5\u627e\u3002<\/p>\n

    \u4e3e\u4e2a\u4f8b\u5b50, \u6709\u4e2a MRO:<\/p>\n

    [A, B, C, D, E, object]<\/pre>\n

    \u4e0b\u9762\u7684\u8c03\u7528:<\/p>\n

    super(C, A).foo()<\/pre>\n

    super \u53ea\u4f1a\u4ece C \u4e4b\u540e\u67e5\u627e\uff0c\u5373: \u53ea\u4f1a\u5728 D \u6216 E \u6216 object \u4e2d\u67e5\u627e foo \u65b9\u6cd5\u3002<\/p>\n

    \u591a\u7ee7\u627f\u4e2d super \u7684\u5de5\u4f5c\u65b9\u5f0f<\/strong><\/div>\n

    \u518d\u56de\u5230\u524d\u9762\u7684<\/p>\n

    d = D()\r\nd.add(2)\r\nprint(d.n)<\/pre>\n

    \u73b0\u5728\u4f60\u53ef\u80fd\u5df2\u7ecf\u6709\u70b9\u7709\u76ee\uff0c\u4e3a\u4ec0\u4e48\u8f93\u51fa\u4f1a\u662f<\/p>\n

    self is <__main__.D object at 0x10ce10e48> @D.add\r\nself is <__main__.D object at 0x10ce10e48> @B.add\r\nself is <__main__.D object at 0x10ce10e48> @C.add\r\nself is <__main__.D object at 0x10ce10e48> @A.add\r\n19<\/pre>\n

    \u4e86\u5427 ;)<\/p>\n

    \u4e0b\u9762\u6211\u4eec\u6765\u5177\u4f53\u5206\u6790\u4e00\u4e0b:<\/p>\n

    D \u7684 MRO \u662f: [D, B, C, A, object] \u3002 \u5907\u6ce8: \u53ef\u4ee5\u901a\u8fc7 D.mro() (Python 2 \u4f7f\u7528 D.__mro__ ) \u6765\u67e5\u770b D \u7684 MRO \u4fe1\u606f\uff09<\/p>\n

    \u8be6\u7ec6\u7684\u4ee3\u7801\u5206\u6790\u5982\u4e0b:<\/p>\n

    class A:\r\n    def __init__(self):\r\n        self.n = 2\r\n\r\n    def add(self, m):\r\n        # \u7b2c\u56db\u6b65\r\n        # \u6765\u81ea D.add \u4e2d\u7684 super\r\n        # self == d, self.n == d.n == 5\r\n        print('self is {0} @A.add'.format(self))\r\n        self.n += m\r\n        # d.n == 7\r\n\r\n\r\nclass B(A):\r\n    def __init__(self):\r\n        self.n = 3\r\n\r\n    def add(self, m):\r\n        # \u7b2c\u4e8c\u6b65\r\n        # \u6765\u81ea D.add \u4e2d\u7684 super\r\n        # self == d, self.n == d.n == 5\r\n        print('self is {0} @B.add'.format(self))\r\n        # \u7b49\u4ef7\u4e8e suepr(B, self).add(m)\r\n        # self \u7684 MRO \u662f [D, B, C, A, object]\r\n        # \u4ece B \u4e4b\u540e\u7684 [C, A, object] \u4e2d\u67e5\u627e add \u65b9\u6cd5\r\n        super().add(m)\r\n\r\n        # \u7b2c\u516d\u6b65\r\n        # d.n = 11\r\n        self.n += 3\r\n        # d.n = 14\r\n\r\nclass C(A):\r\n    def __init__(self):\r\n        self.n = 4\r\n\r\n    def add(self, m):\r\n        # \u7b2c\u4e09\u6b65\r\n        # \u6765\u81ea B.add \u4e2d\u7684 super\r\n        # self == d, self.n == d.n == 5\r\n        print('self is {0} @C.add'.format(self))\r\n        # \u7b49\u4ef7\u4e8e suepr(C, self).add(m)\r\n        # self \u7684 MRO \u662f [D, B, C, A, object]\r\n        # \u4ece C \u4e4b\u540e\u7684 [A, object] \u4e2d\u67e5\u627e add \u65b9\u6cd5\r\n        super().add(m)\r\n\r\n        # \u7b2c\u4e94\u6b65\r\n        # d.n = 7\r\n        self.n += 4\r\n        # d.n = 11\r\n\r\n\r\nclass D(B, C):\r\n    def __init__(self):\r\n        self.n = 5\r\n\r\n    def add(self, m):\r\n        # \u7b2c\u4e00\u6b65\r\n        print('self is {0} @D.add'.format(self))\r\n        # \u7b49\u4ef7\u4e8e super(D, self).add(m)\r\n        # self \u7684 MRO \u662f [D, B, C, A, object]\r\n        # \u4ece D \u4e4b\u540e\u7684 [B, C, A, object] \u4e2d\u67e5\u627e add \u65b9\u6cd5\r\n        super().add(m)\r\n\r\n        # \u7b2c\u4e03\u6b65\r\n        # d.n = 14\r\n        self.n += 5\r\n        # self.n = 19\r\n\r\nd = D()\r\nd.add(2)\r\nprint(d.n)<\/pre>\n

    \u8c03\u7528\u8fc7\u7a0b\u56fe\u5982\u4e0b:<\/p>\n

    D.mro() == [D, B, C, A, object]\r\nd = D()\r\nd.n == 5\r\nd.add(2)\r\n\r\nclass D(B, C):          class B(A):            class C(A):             class A:\r\n    def add(self, m):       def add(self, m):      def add(self, m):       def add(self, m):\r\n        super().add(m)  1.--->  super().add(m) 2.--->  super().add(m)  3.--->  self.n += m\r\n        self.n += 5   <------6. self.n += 3    <----5. self.n += 4     <----4. <--|\r\n        (14+5=19)               (11+3=14)              (7+4=11)                (5+2=7)<\/pre>\n

    \"\"<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u8bf4\u5230 super\uff0c \u5927\u5bb6\u53ef\u80fd\u89c9\u5f97\u5f88\u7b80\u5355\u5440\uff0c\u4e0d\u5c31\u662f\u7528\u6765\u8c03\u7528\u7236\u7c7b\u65b9\u6cd5\u7684\u561b\u3002\u5982\u679c\u771f\u7684\u8fd9\u4e48\u7b80\u5355\u7684\u8bdd\u4e5f\u5c31\u4e0d\u4f1a\u6709\u8fd9\u7bc7\u6587\u7ae0\u4e86 […]<\/p>\n","protected":false},"author":1481,"featured_media":221484,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221481","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221481"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221481\/revisions"}],"predecessor-version":[{"id":221487,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221481\/revisions\/221487"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/221484"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}