Question

Why Python Classes Inherit from object

pythonclassoopobjectinheritance

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 as class 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 MyOtherClass(object):
    pass

For new Python 3 code, the shorter class MyClass: form is usually preferred. You may still see (object) in codebases that support old Python versions, were migrated from Python 2, or follow an older style.

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.

Take Quiz

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 describe(self):
        return f"Book: {self.title}"

You can verify that a Python 3 class is derived from object:

class Book:
    pass

print(issubclass(Book, object))  # True
print(isinstance(Book(), object))  # True

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:

  1. class Message: defines a new class named Message.
  2. Python automatically makes Message a subclass of object.
  3. Message() creates an instance and stores it in message.
  4. issubclass(Message, object) checks whether Message inherits from object; it returns True.
  5. isinstance(message, object) checks whether the created instance is an object; it also returns True.

Output:

True
True

The same result occurs if the declaration is class Message(object):.

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.

Take Quiz

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 __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed

super() follows Python’s inheritance rules to call the appropriate parent implementation. Modern Python classes support this naturally because they all use the object-based class model.

Avoid unnecessary inheritance

Do not inherit from object merely to make a class look formal. It adds no behavior beyond what Python 3 already provides.

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:

dog = Dog()

The parentheses have different meanings depending on where they appear.

Inheriting when composition is clearer

Avoid creating a subclass only to reuse a small unrelated utility. For example, a Report is not naturally a FileWriter; it may instead contain or use one.

class Report:
    def __init__(self, writer):
        self.writer = writer

Use inheritance for a genuine “is a” relationship or when an API requires a particular base class.

Comparisons

Declaration or approachMeaning in Python 3Typical use
class MyClass:Implicitly inherits from objectPreferred for ordinary new classes
class MyClass(object):Explicitly inherits from objectLegacy code or older style
class Dog(Animal):Inherits behavior from AnimalA specialized version of a parent type
class AppError(Exception):Creates a custom exception typeErrors that callers can catch specifically
CompositionOne object stores or uses another object“Has a” relationships

Inheritance describes an is-a relationship:

class Dog(Animal):
    pass

A dog is an animal.

Composition describes a has-a relationship:

class Car:
    def __init__(self, engine):
        self.engine = engine

A car has an engine.

Take Quiz

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
  • object is the root class of normal Python classes.
  • Python 3 classes inherit from object automatically.
  • 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.

Is object the same as an instance of a class?

No. object is a class. An instance is a value created from a class, such as MyClass().

Take Quiz

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.

Take Quiz

Keep learning

Related questions

View more