0

In Python 3 I can do the following:

>>> class A:
...     pass
...
>>> A.mro()
[<class '__main__.A'>, <class 'object'>]

But in Python 2.7.16 I get an AttributeError:

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

I understand that if we convert to "new" style objects (inheriting from object) this issue goes away:

>>> class A(object):
...     pass
...
>>> A.mro()
[<class '__main__.A'>, <type 'object'>]

But my use case is in pdb, and I am dealing with lots of objects which would require lots of refactoring, is there any way to access the MRO with old-style classes?

shayaan
  • 1,154
  • 10
  • 26
  • 2
    Related: https://stackoverflow.com/q/4015417/1324033 - "In Python 2: always inherit from object explicitly." – Sayse Aug 30 '19 at 15:41
  • At this point, an old-style class should be considered a bug, not something to work around. Python 2 is all but dead. I'm being mostly, but not entirely, facetious when I say this question is more appropriate for retrocomputing.stackexchange.com than Stack Overflow. – chepner Aug 30 '19 at 15:57

1 Answers1

1

There is no explicit resolution order for old-style classes; instead, method resolution depends on the transitive closure of the set of parent classes.

From The Python 2.3 Method Resolution Order (emphasis mine):

First of all, let me point out that what I am going to say only applies to the new style classes introduced in Python 2.2: classic classes maintain their old method resolution order, depth first and then left to right.

The resolution order, if exposed at all as explicit data, would have been an implementation detail, not part of the defined interface by the language.

chepner
  • 389,128
  • 51
  • 403
  • 529