Why won't python 2.7 define a class without an inherited object will not have a __mro__ method?

I am working on Mac OS X Yosemite with python 2.7.9.

Here is what I tried:

  • define class

    class A:
        def test(self):
            print "test"
    

    then run

    A.__mro__
    

    then i got

    >>> A.__mro__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: class A has no attribute '__mro__'
    
  • Then i define

    class B(object):
        def test(self):
            print "test"
    

    then run

    B.__mro__
    

    then i got

    >>> B.__mro__
    (<class '__main__.B'>, <type 'object'>)
    

What is the difference between these two definitions? I found that in python 3 in the edition without an object there is a method __mro__.

+4
source share
2 answers

__mro__defined only for new style classes . In Python 2, a class is only new if it inherits from object(or from a built-in type, which, in turn, inherits from object), and all classes in Python 3 are a new style, no matter what.

+3
source

__mro__ , Python object.

Python 3 . object . , Python 2 .

+1

All Articles