Skip to main content

Object-Oriented Programming (OOP) in Python: A Complete Beginner's Guide

Updated 6 min readMarkdown
Object-Oriented Programming (OOP) in Python: A Complete Beginner's Guide

Introduction

Python is one of the most popular programming languages used in software development, data science, machine learning, and artificial intelligence.

As Python programs become larger and more complex, writing everything using functions and variables can become difficult to manage.

This is where Object-Oriented Programming (OOP) comes into the picture.

OOP is a programming paradigm that organizes code around objects rather than just functions and instructions.

In this article, we will understand the fundamentals of OOP in Python, including classes, objects, attributes, methods, constructors, and different types of attributes and methods.

1. What is Object-Oriented Programming?

Object-Oriented Programming is a programming approach in which we organize our code using classes and objects.

An object represents an entity that contains:

  • Data (attributes)
  • Behaviour (methods)

For example, consider a car.

A car has attributes such as:

  • Brand
  • Colour
  • Speed

It also has behaviours such as:

  • Start
  • Stop
  • Accelerate

In OOP, we can represent a car as an object.

2. What is a Class?

A class is a blueprint or template used to create objects.

It defines the attributes and methods that its objects can have.

Let's create a simple class in Python.

class Car:
    def start(self):
        print("Car started")

Here:

  • class is the keyword used to define a class.
  • Car is the name of the class.
  • start() is a method defined inside the class.
  • self refers to the current object.

A class itself is not a specific car. It is simply the blueprint for creating cars.

3. What is an Object?

An object is an instance of a class.

We can create an object by calling the class.

class Car:
    def start(self):
        print("Car started")


car1 = Car()

car1.start()

Output:

Car started

In this example, car1 is an object of the Car class.

We can create multiple objects from the same class.

car1 = Car()
car2 = Car()

Both objects are created using the same blueprint, but they are separate instances.

4. Understanding Attributes and Methods

Attributes represent the data associated with an object.

Methods represent the functions defined inside a class.

Let's understand this with an example.

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def introduce(self):
        print(f"My name is {self.name}")
        print(f"My age is {self.age}")


student1 = Student("Dhiti", 22)

student1.introduce()

Output:

My name is Dhiti
My age is 22

Here:

  • name and age are attributes.
  • introduce() is a method.
  • student1 is an object of the Student class.

5. Types of Attributes in Python

Attributes are variables associated with a class or an object.

In Python, attributes are commonly classified into two types:

5.1 Instance Attributes

Instance attributes belong to a particular object.

Each object can have its own values for instance attributes.

Example:

class Student:
    def __init__(self, name, age):
        self.name = name
        self.age = age


student1 = Student("Dhiti", 22)
student2 = Student("Amit", 24)

print(student1.name)
print(student2.name)

Output:

Dhiti
Amit

Here, name and age are instance attributes.

The values of these attributes can be different for each object.

5.2 Class Attributes

Class attributes are defined directly inside a class, outside its methods.

They are shared by instances of the class unless an instance attribute with the same name overrides them.

Example:

class Student:
    college = "IIT Bombay"

    def __init__(self, name):
        self.name = name


student1 = Student("Dhiti")
student2 = Student("Amit")

print(student1.college)
print(student2.college)

Output:

IIT Bombay
IIT Bombay

Here:

  • college is a class attribute.
  • name is an instance attribute.

Both students share the same class attribute.

6. Types of Methods in Python

Methods are functions defined inside a class.

Python commonly has three types of methods:

  • Instance methods
  • Class methods
  • Static methods

6.1 Instance Methods

Instance methods operate on a particular object.

They take self as their first parameter.

Example:

class Student:
    def __init__(self, name):
        self.name = name

    def display(self):
        print(f"Student name: {self.name}")


student1 = Student("Dhiti")

student1.display()

Output:

Student name: Dhiti

Here, display() is an instance method.

It accesses the instance attribute name using self.

6.2 Class Methods

Class methods operate on the class itself rather than a particular instance.

They use the @classmethod decorator and take cls as their first parameter.

Example:

class Student:
    college = "IIT Bombay"

    @classmethod
    def change_college(cls, new_college):
        cls.college = new_college


Student.change_college("University of Mumbai")

print(Student.college)

Output:

University of Mumbai

Here:

  • @classmethod is a decorator.
  • cls refers to the class.
  • change_college() modifies the class attribute.

6.3 Static Methods

Static methods do not receive self or cls automatically.

They are defined using the @staticmethod decorator.

They are useful when a function is logically related to a class but does not need to access instance or class attributes.

Example:

class Calculator:
    @staticmethod
    def add(a, b):
        return a + b


result = Calculator.add(10, 20)

print(result)

Output:

30

Here, add() is a static method.

It performs a calculation without accessing any instance or class-specific data.

7. What is a Constructor in Python?

A constructor-like initialization method is used to initialize an object when it is created.

In Python, __init__() is commonly called the constructor, although technically it is the initializer. The actual object creation is handled by __new__().

Example:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary


employee1 = Employee("Rahul", 50000)

print(employee1.name)
print(employee1.salary)

Output:

Rahul
50000

When we create the object, Python automatically calls __init__() to initialize its attributes.

8. What is self in Python?

self refers to the current instance of the class.

It allows us to access the attributes and methods associated with that particular object.

class Student:
    def __init__(self, name):
        self.name = name

    def display(self):
        print(self.name)


student1 = Student("Amit")
student2 = Student("Neha")

student1.display()
student2.display()

Output:

Amit
Neha

The self.name attribute belongs to the individual object.

Therefore, student1 and student2 can store different values.

Conclusion

Object-Oriented Programming is an important concept in Python that helps developers write structured, reusable, and maintainable code.

Understanding classes, objects, attributes, methods, constructors, and the different types of attributes and methods provide a strong foundation for learning advanced Python concepts.

As we move towards more complex applications, OOP becomes an important tool for designing software systems.

In the next article, we will explore the four fundamental pillars of OOP in Python: Encapsulation, Inheritance, Abstraction, and Polymorphism.

About the author

  • I'm a software engineer, entrepreneur, and lifelong learner. Beyond coding, I'm interested in sports and business. This blog is where I share my journey, technical insights, and experiences from building products and startups.

No comments yet. Be the first to comment.