10

I am working on Mac OS X v10.10 (Yosemite) with Python 2.7.9.

Here is what I have tried:

  1. Define a 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__'
    
  2. 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 different between the two definitions?

I found that in Python 3, the edition without "object" still has the __mro__ method.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Da Tong
  • 2,018
  • 18
  • 25
  • http://stackoverflow.com/questions/54867/what-is-the-difference-between-old-style-and-new-style-classes-in-python – Holloway Mar 10 '15 at 15:46

2 Answers2

6

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

Community
  • 1
  • 1
jwodder
  • 54,758
  • 12
  • 108
  • 124
1

The __mro__ is only defined for new style classes, those that inherit from Python object.

In Python 3, all classes are new-style classes. They inherit from object implicitly. In other words, old-style Python 2 classes have been removed from the language.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343