第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

新類型類中的方法解析順序(MRO)?

新類型類中的方法解析順序(MRO)?

翻閱古今 2019-10-13 08:00:06
新類型類中的方法解析順序(MRO)?在書中簡略的Python(第2版)有一個例子舊的樣式類來演示如何按照經(jīng)典的解析順序和與新秩序有何不同?通過用新樣式重寫示例,我嘗試了同一個示例,但結(jié)果與用舊樣式類獲得的結(jié)果沒有什么不同。我在運行示例時使用的python版本是2.5.2.下面是一個例子:class Base1(object):       def amethod(self): print "Base1"  class Base2(Base1):       passclass Base3(object):       def amethod(self): print "Base3"class Derived(Base2,Base3):       passinstance = Derived()  instance.amethod()  print Derived.__mro__打電話instance.amethod()版畫Base1,但根據(jù)我對具有新類型類的MRO的理解,輸出應(yīng)該是Base3..打電話Derived.__mro__指紋:(<class '__main__.Derived'>, <class '__main__.Base2'>, <class '__main__.Base1'>, <class '__main__.Base3'>, <type 'object'>)我不確定我對MRO的理解是錯誤的,還是我做了一個我無法察覺的愚蠢的錯誤。請幫助我更好地理解MRO。
查看完整描述

3 回答

?
蝴蝶刀刀

TA貢獻1801條經(jīng)驗 獲得超8個贊

Python的方法解析順序?qū)嶋H上比僅僅理解鉆石模式更復(fù)雜。到真的理解它,看看C3線性化..我發(fā)現(xiàn)在擴展方法以跟蹤訂單時使用print語句確實有幫助。例如,您認為這種模式的輸出是什么?(注意:“X”應(yīng)該是兩個交叉邊,而不是一個節(jié)點,^表示調(diào)用Super()的方法。)

class G():
    def m(self):
        print("G")class F(G):
    def m(self):
        print("F")
        super().m()class E(G):
    def m(self):
        print("E")
        super().m()class D(G):
    def m(self):
        print("D")
        super().m()class C(E):
    def m(self):
        print("C")
        super().m()class B(D, E, F):
    def m(self):
        print("B")
        super().m()class A(B, C):
    def m(self):
        print("A")
        super().m()#      A^#     / \#    B^  C^#   /| X# D^ E^ F^#  \ | /#    G

你拿到B、D、C、E、F、G了嗎?

x = A()x.m()

經(jīng)過多次嘗試,我對C3線性化提出了一個非正式的圖論解釋:(如果這是錯誤的,請告訴我。)

考慮這個例子:

class I(G):

    def m(self):

        print("I")

        super().m()


class H():

    def m(self):

        print("H")


class G(H):

    def m(self):

        print("G")

        super().m()


class F(H):

    def m(self):

        print("F")

        super().m()


class E(H):

    def m(self):

        print("E")

        super().m()


class D(F):

    def m(self):

        print("D")

        super().m()


class C(E, F, G):

    def m(self):

        print("C")

        super().m()


class B():

    def m(self):

        print("B")

        super().m()


class A(B, C, D):

    def m(self):

        print("A")

        super().m()


# Algorithm:


# 1. Build an inheritance graph such that the children point at the parents (you'll have to imagine the arrows are there) and

#    keeping the correct left to right order. (I've marked methods that call super with ^)


#          A^

#       /  |  \

#     /    |    \

#   B^     C^    D^  I^

#        / | \  /   /

#       /  |  X    /   

#      /   |/  \  /     

#    E^    F^   G^

#     \    |    /

#       \  |  / 

#          H

# (In this example, A is a child of B, so imagine an edge going FROM A TO B)


# 2. Remove all classes that aren't eventually inherited by A


#          A^

#       /  |  \

#     /    |    \

#   B^     C^    D^

#        / | \  /  

#       /  |  X    

#      /   |/  \ 

#    E^    F^   G^

#     \    |    /

#       \  |  / 

#          H


# 3. For each level of the graph from bottom to top

#       For each node in the level from right to left

#           Remove all of the edges coming into the node except for the right-most one

#           Remove all of the edges going out of the node except for the left-most one


# Level {H}

#

#          A^

#       /  |  \

#     /    |    \

#   B^     C^    D^

#        / | \  /  

#       /  |  X    

#      /   |/  \ 

#    E^    F^   G^

#               |

#               |

#               H


# Level {G F E}

#

#         A^

#       / |  \

#     /   |    \

#   B^    C^   D^

#         | \ /  

#         |  X    

#         | | \

#         E^F^ G^

#              |

#              |

#              H


# Level {D C B}

#

#      A^

#     /| \

#    / |  \

#   B^ C^ D^

#      |  |  

#      |  |    

#      |  |  

#      E^ F^ G^

#            |

#            |

#            H


# Level {A}

#

#   A^

#   |

#   |

#   B^  C^  D^

#       |   |

#       |   |

#       |   |

#       E^  F^  G^

#               |

#               |

#               H


# The resolution order can now be determined by reading from top to bottom, left to right.  A B C E D F G H


x = A()

x.m()



查看完整回答
反對 回復(fù) 2019-10-14
?
冉冉說

TA貢獻1877條經(jīng)驗 獲得超1個贊

你得到的結(jié)果是正確的。嘗試更改Base3Base1并與經(jīng)典類的相同層次結(jié)構(gòu)進行比較:

class Base1(object):
    def amethod(self): print "Base1"class Base2(Base1):
    passclass Base3(Base1):
    def amethod(self): print "Base3"class Derived(Base2,Base3):
    passinstance = Derived()instance.amethod()class Base1:
    def amethod(self): print "Base1"class Base2(Base1):
    passclass Base3(Base1):
    def amethod(self): print "Base3"class Derived(Base2,Base3):
    passinstance = Derived()instance.amethod()

現(xiàn)在它的產(chǎn)出是:

Base3Base1

朗讀,閱讀這種解釋想了解更多信息。



查看完整回答
反對 回復(fù) 2019-10-14
  • 3 回答
  • 0 關(guān)注
  • 417 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號