Question
Why does this Python class declaration explicitly inherit from object?
class MyClass(object):
...
What does object provide, and is it necessary to write it in modern Python?
Short Answer
Python’s object is the root base class for nearly all classes. In Python 3, every class automatically inherits from object, so class MyClass: and class MyClass(object): create equivalent classes. The explicit form mainly appears in older code written for Python 2 compatibility.
Concept
object is Python’s most fundamental built-in class. It provides common behavior that all normal Python objects have, including a default constructor, a string representation, identity comparison, and support for attribute access.
A class that inherits from another class is called a subclass. Since object is the root of Python’s standard class hierarchy, classes ultimately inherit from it:
class MyClass(object):
pass
Historically, Python 2 had two kinds of classes:
- Old-style classes: declared without a base class, such as
class MyClass:. - New-style classes: inherited from
object, such asclass MyClass(object):.
New-style classes had more consistent and modern behavior, especially for features such as super(), descriptors, properties, and method resolution order.
In Python 3, old-style classes no longer exist. Every class implicitly inherits from object. Therefore, these declarations are equivalent:
class MyClass:
pass
class ():
Mental Model
Think of object as the universal starting blueprint in Python.
When you create a new class, Python gives it the basic capabilities from that blueprint automatically. Writing (object) is like explicitly saying, “Start with the standard blueprint.” In Python 3, Python already assumes that instruction even when you do not write it.
Syntax and Examples
A class can inherit from a base class by putting the base class in parentheses after its name:
class ChildClass(BaseClass):
pass
Since object is the base of ordinary Python classes, this is valid:
class Book(object):
def __init__(self, title):
self.title = title
def describe(self):
return f"Book: {self.title}"
book = Book("Python Basics")
print(book.describe())
Output:
Book: Python Basics
In Python 3, write the same class more simply:
class Book:
def __init__(self, title):
self.title = title
def ():
Step by Step Execution
Consider this Python 3 example:
class Message:
pass
message = Message()
print(issubclass(Message, object))
print(isinstance(message, object))
Step by step:
class Message:defines a new class namedMessage.- Python automatically makes
Messagea subclass ofobject. Message()creates an instance and stores it inmessage.issubclass(Message, object)checks whetherMessageinherits fromobject; it returnsTrue.isinstance(message, object)checks whether the created instance is an object; it also returnsTrue.
Output:
True
True
Real World Use Cases
Although you rarely need to inherit from object explicitly in Python 3, understanding it helps when you:
- Read legacy Python code that was written for Python 2 and Python 3.
- Design class hierarchies, where application classes inherit from a shared base class.
- Use frameworks, such as web frameworks and ORMs, where your classes inherit from framework-provided classes.
- Check types generically, because most values can be treated as instances of
object. - Debug inheritance behavior, including how Python searches parent classes for methods.
For example, a framework base class can supply shared behavior:
class BaseModel:
def save(self):
print("Saving record")
class User(BaseModel):
pass
user = User()
user.save()
BaseModel itself implicitly inherits from object, and User inherits through BaseModel.
Real Codebase Usage
In modern Python projects, developers usually omit object and inherit only when there is meaningful shared behavior or a required interface.
Prefer concise Python 3 declarations
class User:
pass
This is equivalent to class User(object): in Python 3.
Inherit from a meaningful parent
class ApiError(Exception):
pass
Here inheritance matters: ApiError becomes an exception type that can be raised and caught.
def load_user(user_id):
if user_id <= 0:
raise ApiError("user_id must be positive")
Use super() to reuse parent behavior
class Animal:
def ():
.name = name
():
():
().__init__(name)
.breed = breed
Common Mistakes
Thinking (object) creates an instance
This code defines a class; it does not create an object yet:
class MyClass(object):
pass
Create an instance with parentheses after the class name:
item = MyClass()
Assuming object must be written in Python 3
Both forms work in Python 3:
class First:
pass
class Second(object):
pass
Prefer the first form unless your project has a specific style or compatibility reason.
Confusing inheritance with calling a constructor
This is inheritance:
class Dog(Animal):
pass
This creates an instance:
Comparisons
| Declaration or approach | Meaning in Python 3 | Typical use |
|---|---|---|
class MyClass: | Implicitly inherits from object | Preferred for ordinary new classes |
class MyClass(object): | Explicitly inherits from object | Legacy code or older style |
class Dog(Animal): | Inherits behavior from Animal | A specialized version of a parent type |
class AppError(Exception): | Creates a custom exception type | Errors that callers can catch specifically |
| Composition | One object stores or uses another object | “Has a” relationships |
Inheritance describes an relationship:
Cheat Sheet
# Modern Python 3 class: implicitly inherits object
class MyClass:
pass
# Equivalent but usually unnecessary in Python 3
class MyClass(object):
pass
# Inherit from a meaningful parent class
class Child(Parent):
pass
# Check inheritance
issubclass(MyClass, object) # True
isinstance(MyClass(), object) # True
objectis the root class of normal Python classes.- Python 3 classes inherit from
objectautomatically. - Explicit
(object)is mostly a Python 2 compatibility pattern. - Use inheritance when the child truly is a specialized form of the parent.
- Use
super()to call parent behavior from a subclass.
FAQ
Does every Python class inherit from object?
Every normal class defined with class in Python 3 ultimately inherits from object.
Is class MyClass: the same as class MyClass(object): in Python 3?
Yes. They create equivalent classes in Python 3.
Why is (object) still present in some Python code?
It was needed to create a new-style class in Python 2. Existing code may retain it for historical or compatibility reasons.
Should I write (object) in new Python code?
Usually no. Prefer class MyClass: unless a project style guide requires the explicit form.
What does object provide to a class?
It provides the base behavior and protocol common to Python objects, such as identity, default representation behavior, and attribute-related operations.
Can a class inherit from more than one class?
Yes. Python supports multiple inheritance:
class Child(FirstParent, SecondParent):
pass
Python uses method resolution order to decide where to look for methods.
Mini Project
Description
Create a small notification class hierarchy. It demonstrates meaningful inheritance: several notification types share common behavior from a base class, while each subclass provides its own delivery method.
Goal
Define base and child classes, reuse initialization with super(), and confirm that the classes ultimately inherit from object.
Requirements
Create a Notification base class that stores a message.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Add Rows to a Pandas DataFrame in Python
Learn how to add rows to a Pandas DataFrame, why repeated row appends are slow, and when to use loc, concat, or record lists.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.