“mro()”是什么意思?做?

发布于 2024-08-17 09:41:59 字数 476 浏览 8 评论 0原文

mro()< 是什么意思/a> 做什么?


示例来自 django.utils.function< /a>:

for t in type(res).mro():  # <----- this
    if t in self.__dispatch:
        return self.__dispatch[t][funcname](res, *args, **kw)

What does mro() do?


Example from django.utils.functional:

for t in type(res).mro():  # <----- this
    if t in self.__dispatch:
        return self.__dispatch[t][funcname](res, *args, **kw)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魂归处 2024-08-24 09:41:59

继续...:

>>> class A(object): pass
... 
>>> A.__mro__
(<class '__main__.A'>, <type 'object'>)
>>> class B(A): pass
... 
>>> B.__mro__
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
>>> class C(A): pass
... 
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <type 'object'>)
>>> 

只要我们有单一继承,__mro__ 只是以下元组:类、其基类、其基类的基类,依此类推,直至 object(当然仅适用于新式课程)。

现在,通过多重继承...:

>>> class D(B, C): pass
... 
>>> D.__mro__
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <type 'object'>)

...您还可以保证,在__mro__中,没有类是重复的,并且没有类出现在其祖先之后,保存首先进入同一多重继承级别的类(如本例中的 B 和 C)位于从左到右的 __mro__ 中。

从概念上讲,您在类实例上获得的每个属性(而不仅仅是方法)都会沿着 __mro__ 进行查找,因此,如果祖先中不止一个类定义了该名称,这会告诉您该属性将在哪里找到 - 在定义该名称的 __mro__ 的第一个类中。

Follow along...:

>>> class A(object): pass
... 
>>> A.__mro__
(<class '__main__.A'>, <type 'object'>)
>>> class B(A): pass
... 
>>> B.__mro__
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)
>>> class C(A): pass
... 
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <type 'object'>)
>>> 

As long as we have single inheritance, __mro__ is just the tuple of: the class, its base, its base's base, and so on up to object (only works for new-style classes of course).

Now, with multiple inheritance...:

>>> class D(B, C): pass
... 
>>> D.__mro__
(<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <type 'object'>)

...you also get the assurance that, in __mro__, no class is duplicated, and no class comes after its ancestors, save that classes that first enter at the same level of multiple inheritance (like B and C in this example) are in the __mro__ left to right.

Every attribute you get on a class's instance, not just methods, is conceptually looked up along the __mro__, so, if more than one class among the ancestors defines that name, this tells you where the attribute will be found -- in the first class in the __mro__ that defines that name.

清醇 2024-08-24 09:41:59

mro() 代表 方法解析订单。它返回类派生自的类型列表,按照搜索方法的顺序排列。

mro()__mro__ 仅适用于新样式类。在 Python 3 中,它们可以正常工作。然而,在 Python 2 中,这些类需要从 object 继承。

mro() stands for Method Resolution Order. It returns a list of types the class is derived from, in the order they are searched for methods.

mro() and __mro__ work only on new style classes. In Python 3, they work without any issues. In Python 2, however, those classes need to inherit from object.

断爱 2024-08-24 09:41:59

这也许会显示解析的顺序。

class A(object):
    def dothis(self):
        print('I am from A class')

class B(A):
    pass

class C(object):
    def dothis(self):
        print('I am from C class')

class D(B, C):
    pass

d_instance = D()
d_instance.dothis()
print(D.mro())

输出将是:

I am from A class
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]

规则是深度优先,在本例中意味着 DBAC< /代码>。

Python 在搜索继承类时通常使用深度优先顺序,但是当两个类继承自同一个类时,Python 会从 MRO 中删除第一次提及该类。

This would perhaps show the order of resolution.

class A(object):
    def dothis(self):
        print('I am from A class')

class B(A):
    pass

class C(object):
    def dothis(self):
        print('I am from C class')

class D(B, C):
    pass

d_instance = D()
d_instance.dothis()
print(D.mro())

The output would be:

I am from A class
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.A'>, <class '__main__.C'>, <class 'object'>]

The rule is depth-first, which in this case would mean D, B, A, C.

Python normally uses a depth-first order when searching inheriting classes, but when two classes inherit from the same class, Python removes the first mention of that class from the MRO.

故事还在继续 2024-08-24 09:41:59

钻石继承中的解析顺序会有所不同。

class A(object):
    def dothis(self):
        print('I am from A class')


class B1(A):
    def dothis(self):
        print('I am from B1 class')
    # pass


class B2(object):
    def dothis(self):
        print('I am from B2 class')
    # pass


class B3(A):
    def dothis(self):
        print('I am from B3 class')


# Diamond inheritance
class D1(B1, B3):
    pass


class D2(B1, B2):
    pass


d1_instance = D1()
d1_instance.dothis()
# I am from B1 class
print(D1.__mro__)
# (<class '__main__.D1'>, <class '__main__.B1'>, <class '__main__.B3'>, <class '__main__.A'>, <class 'object'>)


d2_instance = D2()
d2_instance.dothis()
# I am from B1 class
print(D2.__mro__)
# (<class '__main__.D2'>, <class '__main__.B1'>, <class '__main__.A'>, <class '__main__.B2'>, <class 'object'>)

Order of resolution will be different in diamond inheritance.

class A(object):
    def dothis(self):
        print('I am from A class')


class B1(A):
    def dothis(self):
        print('I am from B1 class')
    # pass


class B2(object):
    def dothis(self):
        print('I am from B2 class')
    # pass


class B3(A):
    def dothis(self):
        print('I am from B3 class')


# Diamond inheritance
class D1(B1, B3):
    pass


class D2(B1, B2):
    pass


d1_instance = D1()
d1_instance.dothis()
# I am from B1 class
print(D1.__mro__)
# (<class '__main__.D1'>, <class '__main__.B1'>, <class '__main__.B3'>, <class '__main__.A'>, <class 'object'>)


d2_instance = D2()
d2_instance.dothis()
# I am from B1 class
print(D2.__mro__)
# (<class '__main__.D2'>, <class '__main__.B1'>, <class '__main__.A'>, <class '__main__.B2'>, <class 'object'>)
ㄟ。诗瑗 2024-08-24 09:41:59

对于 @stryker 的示例,C3 算法为:

L[D(B,C)] = D + merge(BAo, Co, BC)
          = D + B + merge(Ao, Co, C)
          = DB + A + merge(o, Co, C)
          = DBA + merge(Co, C)
          = DBACo

请参阅 Python 2.3 方法决议顺序 | Python.org

For @stryker 's example, the C3 algorithm is:

L[D(B,C)] = D + merge(BAo, Co, BC)
          = D + B + merge(Ao, Co, C)
          = DB + A + merge(o, Co, C)
          = DBA + merge(Co, C)
          = DBACo

See The Python 2.3 Method Resolution Order | Python.org

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文