1. Introduction
A class is a blueprint for creating objects. It bundles data (attributes) and behavior (methods) into a single unit. An object (also called an instance) is a concrete realization of that blueprint, with its own copy of the attributes defined by the class. Python is a fully object-oriented language: even integers, strings, and functions are objects of some class.
Cricket analogy: A 'Bowler' blueprint defines what every bowler has (pace, variations), and Jasprit Bumrah is one concrete object built from it with his own values — and in Python even a simple number like his economy rate, 6, is itself an object of some class.
Classes let you model real-world entities in code. For example, a Car class can describe that every car has a brand, a speed, and can accelerate(). Each individual car object (a Toyota, a Honda) then has its own values for those attributes while sharing the same behavior defined once in the class.
Cricket analogy: A 'Player' class describes that every cricketer has a batting_style and can play_shot(), and Virat Kohli and Rohit Sharma are separate objects each with their own batting_style value while sharing the same play_shot() method defined once.
2. Syntax
class ClassName:
# class body: attributes and methods
def __init__(self, param1, param2):
self.attr1 = param1
self.attr2 = param2
def method_name(self):
# method body
return self.attr1
# Creating an object (instantiation)
obj = ClassName(value1, value2)
obj.method_name()3. Explanation
The class keyword starts a class definition, followed by the class name (conventionally in CapWords style) and a colon. The class body contains method definitions, which are just functions defined inside the class. Calling the class name like a function, e.g. ClassName(value1, value2), creates a new object and automatically invokes __init__ to initialize it.
Cricket analogy: Writing class FastBowler: names a new type in CapWords style, and calling FastBowler('Bumrah', 145) like a function creates a new bowler object and automatically runs __init__ to set his name and pace.
Every object created from a class has its own independent set of instance attributes (unless explicitly shared), but all objects of the same class share the same method definitions stored on the class itself. Attribute access uses dot notation: obj.attr1 reads the attribute, and obj.method_name() calls a method bound to that specific object.
Cricket analogy: Kohli's runs=8000 and Rohit's runs=9000 are independent instance attributes, but both share the exact same play_shot() method defined once on the Player class, and you read kohli.runs or call kohli.play_shot() using dot notation.
In Python, everything is an object, including classes themselves. A class is an instance of a special class called type. This is why type(ClassName) returns <class 'type'>, while type(obj) returns <class 'ClassName'>.
Class variables (defined directly in the class body, outside any method) are shared across all instances. If a class variable holds a mutable object like a list or dict, and one instance mutates it in place (e.g. self.shared_list.append(x)), the change is visible to every other instance — a very common source of bugs. Define per-instance mutable defaults inside __init__ instead.
4. Example
class Car:
wheels = 4 # class variable, shared by all Car objects
def __init__(self, brand, speed):
self.brand = brand # instance variable
self.speed = speed # instance variable
def accelerate(self, amount):
self.speed += amount
return f"{self.brand} is now going {self.speed} km/h"
car1 = Car("Toyota", 60)
car2 = Car("Honda", 80)
print(car1.accelerate(20))
print(car2.accelerate(10))
print("car1 wheels:", car1.wheels)
print("car2 wheels:", car2.wheels)
print("Same class?", type(car1) is type(car2))5. Output
Toyota is now going 80 km/h
Honda is now going 90 km/h
car1 wheels: 4
car2 wheels: 4
Same class? True6. Key Takeaways
- A class is a blueprint; an object (instance) is a concrete realization of that blueprint.
- Instantiation calls the class like a function, which triggers
__init__to set up instance state. - Instance attributes belong to individual objects; class attributes are shared by all instances.
- Mutable class-level defaults are a common trap because they are shared, not copied, per instance.
- In Python, classes themselves are objects — instances of the built-in
typeclass.
Practice what you learned
1. What is the correct term for a concrete realization of a class in Python?
2. In the Car example, why do both car1.wheels and car2.wheels print 4?
3. What happens when you call ClassName(args)?
4. Why is defining a mutable default like a list as a class variable considered risky?
5. What does type(car1) is type(car2) evaluate to when both are Car instances?
6. Which statement best describes a class in Python's object model?
Was this page helpful?
You May Also Like
Constructors in Python
How __init__ initializes new objects, why it isn't really the constructor, and how default values work.
self Keyword in Python
Why self appears as the first parameter of instance methods and how it links a method call back to its object.
Inheritance in Python
How subclasses reuse and extend behavior from parent classes, and how super() and MRO resolve method lookups.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics