本文介绍了mro方法和类的__mro__属性之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 __ metaclass__ = abc.ABCMeta 时,偶然发现了这种多余的,不加下划线的 mro 方法。它似乎与 __ mro __ 相同,只是它返回一个列表而不是一个元组。这是一个随机示例():

I stumbled across this extra, no-underscores mro method when I was using __metaclass__ = abc.ABCMeta. It seems to be the same as __mro__ except that it returns a list instead of a tuple. Here's a random example (ideone snippet):

import abc
import copy

class Life(object):
    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def reproduce(self):
        pass

class Bacterium(Life):
    def reproduce(self):
        return copy.deepcopy(self)

wiggly = Bacterium()

print wiggly.__class__.__mro__
# (<class '__main__.Bacterium'>, <class '__main__.Life'>, <type 'object'>)

print wiggly.__class__.mro()
# [<class '__main__.Bacterium'>, <class '__main__.Life'>, <type 'object'>]

后来,这不是 ABCMeta 所独有的,而是在所有新类中都可用。

I found later that this isn't unique to ABCMeta but is available in all new-style classes.

所以。为什么?

推荐答案

直接来自于什么,这是什么意思, __ mro __ 不是? :

Directly from the documentation:

对我来说很不言自明...

Pretty self-explanatory to me...

实例化时调用mro()并将其结果存储在 __ mro __ 中。它们的目的实际上并不相同。

mro() is called on instanciation and stores its result in __mro__. They don't really have the same purpose.

这篇关于mro方法和类的__mro__属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 19:29