Why Python classes inherit from object
Understand implicit object inheritance in Python 3, the historical Python 2 distinction, and the limits of default equality, hashing, and super.
In Python 3, class Example: implicitly inherits from object. For an ordinary class with no other bases, writing class Example(object): is redundant. You may still see it in code written to support Python 2.
Inspect the inheritance chain
class Example:
pass
print(Example.__mro__)
# (<class '__main__.Example'>, <class 'object'>) when run as a script
print(isinstance(Example(), object)) # True
An explicitly declared base class also ultimately inherits from object. You do not need to append object to every base list; redundant or conflicting base declarations can cause MRO problems.
Why old code writes it explicitly
Python 2 distinguished old-style classes from new-style classes. Inheriting from object, directly or through another new-style class, selected the newer model, including different descriptor and method-resolution behavior. Python 3 removed that old-style distinction.
For maintained Python 3 code, follow the project's conventions and normally omit an unnecessary (object). Retaining the spelling in a historical compatibility codebase does not mean Python 2 remains a supported deployment target.
What object supplies
object provides default behavior for construction and initialization, representation, attribute access, equality, and hashing. Subclasses can override or disable that behavior.
For example, a simple user-defined class is hashable by default, but not every object can be a dictionary key:
class Record:
def __init__(self, value):
self.value = value
def __eq__(self, other):
if not isinstance(other, Record):
return NotImplemented
return self.value == other.value
print(Record.__hash__ is None) # True
Defining equality without a corresponding hash disables the inherited hash in this example. Built-in lists and dictionaries are also unhashable. Any custom hash must remain consistent with equality and stable while the object is used as a key.
FAQ
Does omitting object break super? No. Zero-argument super() works in the appropriate method context without explicitly listing object. It still needs a next method in the MRO and valid arguments; it is not valid in every arbitrary location merely because a class exists.
Where does object appear in the MRO? It is the ultimate base in the ordinary class hierarchy. Python's normal class machinery uses C3 linearization; incompatible multiple-inheritance orders are rejected. Inspect ClassName.__mro__ when reasoning about a specific hierarchy.
How does object relate to type? object is the universal base class, while type is the usual metaclass. type(object) is type and type(type) is type. A class with a custom metaclass can have a type other than type itself.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan