DEV Community

Feddy Mwanjumwa
Feddy Mwanjumwa

Posted on

Python OOP: How Classes and Objects Finally Started Making Sense

Object-Oriented Programming, or OOP, was one of the Python topics that looked complicated before I actually used it.

I kept seeing words like classes, objects, attributes, methods, and inheritance.

At first, I was asking myself, "Why can't I just use functions and variables?"

After practicing, I started to understand that OOP is mainly about organising related data and behaviour together.

What is OOP?

OOP stands for Object-Oriented Programming.

The basic idea is to create objects that contain information and actions related to that object.

For example, if I'm building a school system, a student has information such as a name, age, and course.

A student can also perform actions, such as introducing themselves.

OOP gives me a way to put all of that together.

Classes

A class is like a blueprint for creating objects.

For example:

class Student:

pass

Right now, the class doesn't do anything.

I'm basically saying, "I want a blueprint called Student."

Creating an object

An object is an actual instance of a class.

For example:

student1 = Student()

student2 = Student()

Now I have two Student objects created from the same class.

This was one distinction I had to understand:

Class → blueprint

Object → actual thing created from the blueprint

init()

A class becomes much more useful when I give its objects some information.

That's where __init__() comes in.

For example:

class Student:

def __init__(self, name, age):

self.name = name

self.age = age

Now I can create students like this:

student1 = Student("Feddy", 20)

student2 = Student("Amina", 21)

Each object gets its own name and age.

What is self?

self was probably one of the strangest things when I first saw OOP.

I eventually understood that self refers to the current object.

For example:

class Student:

def __init__(self, name, age):

self.name = name

self.age = age

When I create:

student1 = Student("Feddy", 20)

self.name belongs to student1.

When I create:

student2 = Student("Amina", 21)

self.name belongs to student2.

So the same class can create objects with different information.

Attributes

Attributes are basically the data stored inside an object.

In this example:

self.name = name

self.age = age

name and age are attributes of the Student object.

I can access them like this:

print(student1.name)

print(student1.age)

The result could be:

Feddy

20

Methods

Methods are functions that belong to a class.

For example:

class Student:

def __init__(self, name, age):

self.name = name

self.age = age

def introduce(self):

print(f"My name is {self.name} and I am {self.age} years old.")

Now I can call the method:

student1.introduce()

The result is:

My name is Feddy and I am 20 years old.

This was where OOP started becoming more practical for me.

The object has data, and it also has behaviour.

Changing attributes

Because objects can store data, I can also change that data.

For example:

student1.age = 21

Now:

print(student1.age)

gives:

21

This can be useful when information changes while the program is running.

A practical example

I can create a simple bank account class.

class BankAccount:

def __init__(self, owner, balance):

self.owner = owner

self.balance = balance

def deposit(self, amount):

self.balance += amount

def show_balance(self):

print(f"{self.owner}'s balance: KSh {self.balance}")

Now I can create an account:

account = BankAccount("Feddy", 5000)

I can deposit money:

account.deposit(2000)

And check the balance:

account.show_balance()

The result is:

Feddy's balance: KSh 7000

This example helped me see why putting data and related functions inside one class can be useful.

Inheritance

Another important OOP concept is inheritance.

Inheritance allows one class to get attributes and methods from another class.

For example, I can create a general class:

class Animal:

def speak(self):

print("Animal makes a sound")

Then create another class based on it:

class Dog(Animal):

pass

Now the Dog class automatically gets the speak() method.

I can do:

dog = Dog()

dog.speak()

And get:

Animal makes a sound

The Dog class inherited the method from Animal.

Overriding a method

A child class can also change how an inherited method behaves.

For example:

class Animal:

def speak(self):

print("Animal makes a sound")

class Dog(Animal):

def speak(self):

print("Dog barks")

Now:

dog = Dog()

dog.speak()

gives:

Dog barks

The Dog class has its own version of speak().

This is called method overriding.

Another practical example

Suppose I'm creating an employee system.

I can start with:

class Employee:

def __init__(self, name, salary):

self.name = name

self.salary = salary

Then I can create a manager class:

class Manager(Employee):

def manage_team(self):

print(f"{self.name} is managing the team.")

Now:

manager = Manager("Feddy", 120000)

I can use the inherited information:

print(manager.salary)

And also the method from Manager:

manager.manage_team()

This is where inheritance started making sense to me. I can reuse code from an existing class instead of starting everything from zero.

What I understood

The main ideas I took from OOP are:

Class → blueprint for creating objects.

Object → an actual instance of a class.

__init__() → sets up the object when it is created.

self → refers to the current object.

Attributes → information stored in the object.

Methods → actions the object can perform.

Inheritance → allows one class to reuse another class's code.

Once I understood these, OOP became much less intimidating.

Final thoughts

OOP took me longer to understand than some of the earlier Python topics.

The syntax wasn't necessarily the hardest part. The difficult part was understanding why I would even want to organise my code this way.

What finally helped was using realistic examples.

A student has information and actions.

A bank account has information and actions.

An employee has information and actions.

That's when I started seeing the point of OOP.

Instead of having data and functions scattered everywhere, I can organise related things into objects.

And that makes much more sense when a program starts getting bigger.

Top comments (0)