100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Classes and Objects in Python

How Python's class statement defines a blueprint for objects, and how instances are created and used.

OOP BasicsBeginner9 min readJul 7, 2026
Analogies

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

python
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

python
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

text
Toyota is now going 80 km/h
Honda is now going 90 km/h
car1 wheels: 4
car2 wheels: 4
Same class? True

6. 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 type class.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#ClassesAndObjectsInPython#Classes#Objects#Syntax#Explanation#OOP#StudyNotes#SkillVeris