DEV Community

Cover image for From Blueprints to Reality: Python Classes and Objects
Mary Nyandia
Mary Nyandia

Posted on

From Blueprints to Reality: Python Classes and Objects

I explored object‑oriented programming (OOP) in Python — a way to structure code using classes (blueprints) and objects (instances of those blueprints). OOP makes programs more organized, reusable, and closer to how we think about real‑world entities.

1.Basic Class Definition
Classes define attributes and methods. Objects are created from classes and can use those methods.

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def describe(self):
        return f"{self.name} is a {self.species}"

dog = Animal("Buddy", "Dog")
cat = Animal("Whiskers", "Cat")
print(dog.describe())
print(cat.describe())

Enter fullscreen mode Exit fullscreen mode

2.Class with Multiple Methods
Methods can check conditions, modify attributes, or return information.

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

    def is_honor_student(self):
        return self.gpa >= 3.5

    def promote(self):
        self.grade += 1
        return f"{self.name} is now in grade {self.grade}"

Enter fullscreen mode Exit fullscreen mode

3.Class vs Instance Attributes
Class attributes are shared by all objects, while instance attributes are unique to each object.

class Car:
    wheels = 4  # Class attribute

    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year

Enter fullscreen mode Exit fullscreen mode

4.Inheritance
Inheritance lets child classes reuse and extend parent class functionality.

class Vehicle:
    def __init__(self, make, color):
        self.make = make
        self.color = color

class Motorcycle(Vehicle):
    def __init__(self, make, color, has_sidecar):
        super().__init__(make, color)
        self.has_sidecar = has_sidecar

Enter fullscreen mode Exit fullscreen mode

5.Encapsulation
Private attributes (__balance) hide sensitive data, and getter methods provide controlled access.

class BankAccount:
    def __init__(self, account_holder, balance):
        self.__balance = balance

    def get_balance(self):
        return self.__balance

Enter fullscreen mode Exit fullscreen mode

6.Special Methods (Dunder Methods)
Special methods like str, eq, and lt customize how objects behave with built‑in functions.

class Person:
    def __str__(self):
        return f"{self.name}, {self.age} years old"

Enter fullscreen mode Exit fullscreen mode

7.Multiple Objects in a List
You can store multiple objects in lists and iterate over them.

books = [Book("1984", "George Orwell", 328),
         Book("The Great Gatsby", "F. Scott Fitzgerald", 180)]
for book in books:
    print(book.show_info())

Enter fullscreen mode Exit fullscreen mode

8.Object Composition
Composition means one object contains another, building complex structures.

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

Enter fullscreen mode Exit fullscreen mode

9.Static and Class Methods
Static methods don’t depend on object data, while class methods can access class‑level attributes.

class MathsHelper:
    @staticmethod
    def add(a, b): return a + b
    @classmethod
    def create_circle(cls, radius): ...

Enter fullscreen mode Exit fullscreen mode

10.Polymorphism
Polymorphism lets different classes implement the same method (area) in their own way.

class Shape: 
    def area(self): raise NotImplementedError

Enter fullscreen mode Exit fullscreen mode

🎯 My Take
Classes and objects are the foundation of OOP in Python. They help you:

  • Organize code into reusable blueprints.
  • Model real‑world entities with attributes and methods.
  • Use inheritance, encapsulation, and polymorphism for powerful designs.

Top comments (0)