9

In Python 2, why are instances of old style classes still instances of object even when they do not explicitly inherit from object?

class OldClass:
    pass

>>> isinstance(OldClass(), object)
True

Before testing this, I would have concluded that isinstance(x, object) == True would imply that x is an instance of a subclass of object and thus an instance of a new style class, but it appears that all objects in Python 2 are instances of object (yes, I know how obvious that sounds).


Digging around further, I found some other seemingly odd behavior:

>>> issubclass(OldClass, object)
False

I was under the impression that isinstance(x, SomeClass) is virtually equivalent to issubclass(x.__class__, SomeClass), but apparently I'm missing something.

24
  • 4
    @Jean-FrançoisFabre How does that answer the question at all? Commented Jun 15, 2017 at 11:46
  • 2
    @Jean-FrançoisFabre Because it's designed that way", what?? This question can be answered properly by someone who knows how old style classes work. If an instance of Foo is also an instance of object, how does "it was designed that way" answer why Foo isn't a subclass of object? Commented Jun 15, 2017 at 11:49
  • 2
    Everything is an instance of object, but old-style classes have a divided implementation. The class itself is an instance of classobj, and its instances are instance objects. Commented Jun 15, 2017 at 11:51
  • 4
    I agree that this is not a good 'duplicate'. Should probably be re-opened. Commented Jun 15, 2017 at 11:52
  • 3
    Someone please answer this question. This comment chain is getting ridiculous. Commented Jun 15, 2017 at 12:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.