Sitemap
SONALI BALLA

SONALI BALLA

Aspiring Gen AI Engineer | Learning, building, and sharing my Generative AI journey.

Understanding Classes, Objects & Reusable Code

5 min readSep 29, 2026

--

Press enter or click to view image in full size

Day 7 of My Generative AI Learning Journey

As I continue my Python journey for Generative AI, I’ve reached an important topic: Object-Oriented Programming (OOP).

At first, terms like class, object, inheritance, encapsulation, and polymorphism can sound complicated.

But the basic idea is actually simple:

OOP helps us organize our code around objects and their behavior.

And as we move toward APIs, AI applications, LLMs, RAG systems, and AI agents, understanding OOP becomes increasingly useful.

What is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming approach where we organize programs using objects.

An object can contain:

  • Data → attributes
  • Behavior → methods

For example, imagine an AI chatbot.

A chatbot could have:

  • name
  • model
  • conversation history

And it could perform actions such as:

  • send a message
  • receive a response
  • clear conversation history

Instead of keeping everything as separate variables and functions, we can organize them inside a class.

What is a Class?

A class is like a blueprint for creating objects.

For example:

class Student:
pass

Here, Student is a class.

Think of a class as a blueprint.

🏠 Blueprint → House
📦 Class → Object

The class describes what an object should contain.

What is an Object?

An object is an instance of a class.

class Student:
pass
student1 = Student()
student2 = Student()

Here:

  • Student → class
  • student1 → object
  • student2 → object

We can create many objects from the same class.

The __init__() Method

The __init__() method is used to initialize an object's data when the object is created.

Example:

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

Now we can create an object:

student1 = Student("Sonali", 22)
print(student1.name)
print(student1.age)

Output:

Sonali
22

What is self?

self refers to the current object.

For example:

self.name = name

means:

Store the value of name inside this particular object.

Different objects can therefore have different values.

student1 = Student("Sonali", 22)
student2 = Student("Rahul", 23)

Each object has its own data.

What are Methods?

A method is a function defined inside a class.

Example:

class Student:
    def __init__(self, name):
self.name = name
def introduce(self):
print(f"Hi, I am {self.name}")

Now:

student1 = Student("Sonali")
student1.introduce()

Output:

Hi, I am Sonali

So:

Attributes → store information

Methods → perform actions

The Four Main Concepts of OOP

The four commonly discussed pillars of OOP are:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Let’s understand them simply.

Encapsulation

Encapsulation means keeping data and the methods that operate on that data together inside a class, while controlling how that data is accessed.

Example:

class BankAccount:
    def __init__(self, balance):
self.balance = balance
def deposit(self, amount):
self.balance += amount
def show_balance(self):
print(self.balance)

Now:

account = BankAccount(5000)
account.deposit(1000)account.show_balance()

Output:

6000

The class controls how the balance is modified.

Inheritance

Inheritance allows one class to reuse properties and methods from another class.

Example:

class Animal:
    def eat(self):
print("Animal is eating")

Create another class:

class Dog(Animal):
    def bark(self):
print("Dog is barking")

Now:

dog = Dog()
dog.eat()
dog.bark()

Output:

Animal is eating
Dog is barking

The Dog class inherited the eat() method from Animal.

Why is inheritance useful?

It helps us:

  • reuse code
  • reduce duplication
  • create relationships between classes
  • organize larger applications

Polymorphism

Polymorphism means that the same method or interface can behave differently depending on the object.

Get SONALI BALLA’s stories in your inbox

Join Medium for free to get updates from this writer.

Example:

class Dog:
    def sound(self):
print("Bark")
class Cat: def sound(self):
print("Meow")

Now:

dog = Dog()
cat = Cat()
dog.sound()
cat.sound()

Output:

Bark
Meow

Both objects have a sound() method, but they behave differently.

Abstraction

Abstraction means hiding unnecessary implementation details and exposing only what is needed.

Think about using an AI application.

You might call:

response = model.generate("Explain AI")

You don’t necessarily need to know every internal step happening inside the model.

The complicated implementation is hidden behind a simpler interface.

That’s the basic idea of abstraction.

Class Variables vs Instance Variables

There are two important types of variables you should understand.

Instance Variable

Belongs to a particular object.

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

Each student can have a different name.

Class Variable

Shared by the class.

class Student:
    school = "ABC School"    def __init__(self, name):
self.name = name

Now:

student1 = Student("Sonali")
student2 = Student("Rahul")
print(student1.school)
print(student2.school)

Both objects can access the same class variable.

Why OOP Matters in Gen AI

This is where OOP becomes interesting for my Generative AI journey.

Gen AI applications can become complex.

A simple application might look like:

User
↓
Input
↓
Prompt
↓
Model
↓
Response

But a real application might involve:

User
↓
Application
↓
Prompt Processing
↓
Embedding Model
↓
Vector Database
↓
Retriever
↓
LLM
↓
Response Processing
↓
User

Instead of writing everything in one huge Python file, we can organize different components using classes.

For example:

class Chatbot:
    def __init__(self, model):
self.model = model
def generate_response(self, prompt):
return self.model.generate(prompt)

This is only a simplified example, but the idea is important.

As Gen AI applications become larger, organized and reusable code becomes extremely valuable.

A Simple Gen AI-Style Example

We can create a simple prompt manager:

class PromptManager:
    def __init__(self, topic):
self.topic = topic
def create_prompt(self):
return f"Explain {self.topic} in simple terms."
manager = PromptManager("Generative AI")prompt = manager.create_prompt()print(prompt)

Output:

Explain Generative AI in simple terms.

Now we have a reusable object for managing prompts.

Later, the same idea can be extended into more complex systems.

OOP in Real Gen AI Applications

OOP can be useful when building components such as:

  • Chatbot classes
  • Prompt managers
  • Document processors
  • Data loaders
  • Embedding utilities
  • Vector database interfaces
  • RAG pipelines
  • AI agents
  • API clients
  • Model wrappers

You don’t need to become an OOP expert before starting Gen AI.

But you should be comfortable reading and writing basic classes.

Mini Practice Challenge

Try creating a class called AIAssistant.

It should contain:

  • name
  • role
  • introduce() method
  • generate_prompt() method

For example:

assistant = AIAssistant(
"GenAI Assistant",
"AI Tutor"
)

The introduce() method could print:

I am GenAI Assistant, your AI Tutor.

And generate_prompt() could create a prompt based on a topic.

This is a small exercise, but it helps connect OOP with your Gen AI learning.

What I Learned Today

Today I learned:

  • What OOP is
  • Classes
  • Objects
  • __init__()
  • self
  • Attributes
  • Methods
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
  • Class variables
  • Instance variables
  • How OOP can be used to organize Gen AI applications

The biggest takeaway for me is:

OOP is not just about learning syntax. It’s about organizing code so that larger applications become easier to build and maintain

What’s Next?

Next, I’ll learn Python Modules and Packages.

This will help me understand how larger Python projects are structured and how we can reuse code across different files.

And eventually, these Python fundamentals will connect with the bigger Gen AI ecosystem.

Python → ML → Deep Learning → Transformers → LLMs → RAG → AI Agents

Question for You

When you first learned OOP, which concept was the most confusing?

Classes, objects, inheritance, polymorphism, encapsulation, or abstraction?

I’d love to know what others found difficult while learning Python.

Tags

Python Generative AI GenAI Artificial Intelligence Machine Learning Programming Python OOP Learning in Public

--

--

SONALI BALLA
SONALI BALLA