Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Const

class

The class statement defines a class object from a name, optional type parameters and bases, and an indented suite. A missing base list defaults to object; multiple bases enable multiple inheritance.

How it works

A class definition is an executable statement that creates a class object when its suite finishes normally. Its suite establishes the class namespace, and an optional parenthesized inheritance list selects base classes; without one, the class inherits from object.

Syntax

class classname [type_params] [inheritance] : suite

Returns

A class object.

Key points

  • The statement produces a class object rather than directly producing an instance.
  • Python executes the definition as part of normal program flow.
  • Omitting parentheses gives the class object the default base object.
  • The syntax supports multiple inheritance by listing several base classes.
  • Put base classes after the name inside parentheses and separate them with commas.

Common mistakes

  • Expressions in the inheritance list are used to select base classes, so they must evaluate to class objects.

Example: Define and use a class

class Counter:
    def __init__(self, start=0):
        self.value = start

    def increment(self):
        self.value += 1

counter = Counter(2)
counter.increment()
print(counter.value)

The class creates instances that store state and expose behavior through methods.

When to use it

Encapsulate state and operations

Model a domain concept with data and behavior.

Create an inheritance hierarchy

Share or specialize behavior across related types.

Build object-oriented APIs

Define reusable objects with a consistent interface.

Interview questions

QWhat does a class definition create?

The class statement defines a class object. It is executable, and its suite runs as part of the definition before the class object is produced.

Follow-up: What happens if there is no inheritance list? The class inherits from object by default.

QHow does class inheritance appear in the syntax?

The inheritance list appears in parentheses after the class name and normally contains base classes. Each item must evaluate to a class object that supports subclassing.

Follow-up: Can several bases be specified? Yes; listing multiple base classes enables multiple inheritance.

QWhat is the grammar of a class definition?

A class definition has the form class, a class name, optional type parameters, optional inheritance, a colon, and a suite. The class name is an identifier.

Follow-up: What is the simplest valid form? class Name: followed by an indented suite, such as pass.

  • object
  • decorators
  • type parameters
  • inheritance
  • multiple inheritance
  • metaclasses

Source: 8. Compound statements. Checked 2026-10-16.