How does the built-in `object` class relate to every class's MRO, and why does it always appear last?
Short answer
Every class in Python 3 implicitly inherits from `object` (directly or transitively), which provides default implementations of fundamental magic methods like __init__, __repr__, __eq__, and __hash__.
Advertisement
AModel answer
Every class in Python 3 implicitly inherits from `object` (directly or transitively), which provides default implementations of fundamental magic methods like __init__, __repr__, __eq__, and __hash__. Because every other class is 'more specific' than object, C3 linearization always places object last in the MRO -- it's the common ancestor that every other class's MRO must be consistent with appearing after.
class Foo:
pass
print(Foo.__mro__) # (<class 'Foo'>, <class 'object'>)
print(issubclass(Foo, object)) # True, implicitlyReal-world example
Understanding that object always terminates the MRO clarifies why calling super().__init__() in a deeply nested class hierarchy eventually reaches object.__init__(), which does nothing but must still be part of the chain.
Likely follow-ups
- What default methods does object provide?
- Why did Python 2 require explicit inheritance from object for 'new-style classes'?
Related topics