Object-Oriented Programming in Python: My Journey with Classes¶
Author: Mohammad Sayem Chowdhury
Welcome! In this notebook, I'll share my personal exploration of classes and objects in Python. I find object-oriented programming fascinating because it mirrors how we think about the real world. I'll create Circle and Rectangle classes to demonstrate key concepts like attributes and methods.
What I'll Cover¶
- Introduction to Classes and Objects
- Creating My Own Classes
- Working with Circle Objects
- Building a Rectangle Class
My Understanding of Classes and Objects¶
When I first learned about classes, I found it helpful to think of them as blueprints. Just like architectural blueprints help build houses, class blueprints help create objects in programming.
Creating a Class: My Approach¶
I like to think of a class as a template that defines what data (attributes) and behaviors (methods) objects will have. For example, if I want to create circles, I need to decide what makes a circle unique - its radius and color are good starting points.
Objects and Attributes: Real-World Thinking¶
An object is like a real thing created from the class blueprint. If Circle is my blueprint, then I can create many circle objects - each with its own specific radius and color. It's like having a cookie cutter (class) that makes different cookies (objects).
Methods: Adding Behavior¶
Methods are functions that belong to a class. They let me interact with and modify objects. For instance, I might want to increase a circle's radius - that's where methods come in handy.
# Importing my visualization tools
import matplotlib.pyplot as plt
%matplotlib inline
Now I'll create my Circle class. The __init__ method is special - it runs automatically when I create a new circle object:
# My Circle class definition
class Circle(object):
# Constructor - this runs when I create a new circle
def __init__(self, radius=3, color='blue'):
self.radius = radius
self.color = color
# Method to increase the radius
def add_radius(self, r):
self.radius = self.radius + r
return(self.radius)
# Method to visualize the circle
def draw_circle(self):
plt.gca().add_patch(plt.Circle((0, 0), radius=self.radius, fc=self.color))
plt.axis('scaled')
plt.show()
Working with Circle Objects: My Experiments¶
Let me create my first circle object. I'll make it red with a radius of 10:
# Creating my red circle
my_red_circle = Circle(10, 'red')
I can explore what methods are available on my circle object:
# Exploring available methods
dir(my_red_circle)
Let me check the attributes of my circle:
# Checking the radius
print(f"My circle's radius: {my_red_circle.radius}")
# Checking the color
print(f"My circle's color: {my_red_circle.color}")
I can modify the attributes directly if needed:
# Changing the radius
my_red_circle.radius = 1
print(f"New radius: {my_red_circle.radius}")
Now let me visualize my circle:
# Drawing my circle
my_red_circle.draw_circle()
I can use methods to modify my circle. Let me increase the radius step by step:
# Experimenting with the add_radius method
print('Starting radius:', my_red_circle.radius)
my_red_circle.add_radius(2)
print('After adding 2:', my_red_circle.radius)
my_red_circle.add_radius(5)
print('After adding 5 more:', my_red_circle.radius)
Let me create another circle. Since blue is the default color, I only need to specify the radius:
# Creating a large blue circle
my_blue_circle = Circle(radius=100)
I can verify its attributes:
# Checking the blue circle's properties
print(f"Blue circle radius: {my_blue_circle.radius}")
print(f"Blue circle color: {my_blue_circle.color}")
# Drawing the blue circle
my_blue_circle.draw_circle()
Building a Rectangle Class: Expanding My Skills¶
Now I'll create a Rectangle class to practice with different attributes:
# My Rectangle class
class Rectangle(object):
# Constructor with width, height, and color
def __init__(self, width=2, height=3, color='red'):
self.height = height
self.width = width
self.color = color
# Method to draw the rectangle
def draw_rectangle(self):
plt.gca().add_patch(plt.Rectangle((0, 0), self.width, self.height, fc=self.color))
plt.axis('scaled')
plt.show()
Let me create a skinny blue rectangle:
# Creating my first rectangle
skinny_blue_rect = Rectangle(2, 10, 'blue')
I'll examine its attributes:
# Checking rectangle properties
print(f"Height: {skinny_blue_rect.height}")
print(f"Width: {skinny_blue_rect.width}")
print(f"Color: {skinny_blue_rect.color}")
# Drawing the skinny rectangle
skinny_blue_rect.draw_rectangle()
Now let me create a contrasting rectangle - short and wide:
# Creating a wide yellow rectangle
wide_yellow_rect = Rectangle(20, 5, 'yellow')
# Checking its properties
print(f"Height: {wide_yellow_rect.height}")
print(f"Width: {wide_yellow_rect.width}")
print(f"Color: {wide_yellow_rect.color}")
# Drawing the wide rectangle
wide_yellow_rect.draw_rectangle()
My Reflections on Object-Oriented Programming¶
Working with classes and objects has taught me to think about code organization in a more structured way. Each class encapsulates related data and behavior, making my code more modular and reusable. The ability to create multiple objects from the same class template is particularly powerful.
What I find most satisfying is how object-oriented programming mirrors real-world thinking - we naturally categorize things and think about their properties and behaviors.
Copyright © 2025 Mohammad Sayem Chowdhury. This notebook and its content are shared for personal learning and inspiration.
Next Steps¶
This foundation in classes opens up many possibilities. I could add more methods to calculate area, perimeter, or even create inheritance relationships between classes. The journey into object-oriented programming is just beginning!
— Mohammad Sayem Chowdhury