Skip to content
Home » Object-Oriented Programming (OOP) in Python: A Beginner’s Guide

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

  • by

Python is a versatile language that supports both procedural and object-oriented programming (OOP). OOP is a powerful paradigm that helps organize code, improve reusability, and manage complexity by modeling real-world entities as objects.

In this guide, you’ll learn the fundamentals of OOP in Python, including classes, objects, methods, inheritance, encapsulation, and polymorphism — all with clear explanations and code examples.


What Is Object-Oriented Programming?

OOP is a way of structuring code by bundling related data and behaviors into reusable blueprints called classes, and actual instances of those blueprints are called objects.

Benefits of OOP:

  • Organizes code into logical components
  • Makes large codebases easier to maintain
  • Encourages reusability through inheritance
  • Helps avoid code duplication

Key OOP Concepts in Python

1. Class and Object

A class is a blueprint, while an object is a real-world instance of that blueprint.

class Dog:
def bark(self):
print("Woof!")

my_dog = Dog() # Creating an object
my_dog.bark() # Calling a method → Output: Woof!

2. Constructor (__init__ Method)

__init__() is called automatically when you create a new object. It initializes object attributes.

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

def bark(self):
print(f"{self.name} says woof!")

dog1 = Dog("Buddy", 3)
dog1.bark() # Buddy says woof!

3. Attributes and Methods

  • Attributes are variables tied to an object (self.name).
  • Methods are functions tied to an object.
print(dog1.name)     # Buddy
dog1.bark() # Buddy says woof!

4. Encapsulation

Encapsulation is the bundling of data and methods into one unit — the class — and controlling access to it.

Private Attributes (convention with _ or __):

class Account:
def __init__(self, balance):
self.__balance = balance # private variable

def deposit(self, amount):
self.__balance += amount

def get_balance(self):
return self.__balance

acc = Account(1000)
acc.deposit(500)
print(acc.get_balance()) # 1500

You can’t access __balance directly from outside.


5. Inheritance

Inheritance lets one class (child) reuse the attributes and methods of another class (parent).

class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def bark(self):
print("Dog barks")

d = Dog()
d.speak() # Inherited from Animal
d.bark()

6. Method Overriding

You can override methods in the child class to change behavior.

class Animal:
def speak(self):
print("Animal speaks")

class Cat(Animal):
def speak(self):
print("Meow")

c = Cat()
c.speak() # Meow

7. Polymorphism

Polymorphism allows different classes to use the same method name with different implementations.

class Bird:
def sound(self):
print("Chirp")

class Duck:
def sound(self):
print("Quack")

def make_sound(animal):
animal.sound()

b = Bird()
d = Duck()

make_sound(b) # Chirp
make_sound(d) # Quack

Summary of OOP Terminology

ConceptDescription
ClassBlueprint for creating objects
ObjectInstance of a class
MethodFunction inside a class
AttributeVariable inside a class
EncapsulationRestricting direct access to data
InheritanceCreating a class based on another
PolymorphismUsing common method names with different behavior

Real-World Example: A Simple Banking App

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

def deposit(self, amount):
self.__balance += amount

def withdraw(self, amount):
if amount > self.__balance:
print("Insufficient funds")
else:
self.__balance -= amount

def get_balance(self):
return self.__balance

account = BankAccount("Alice", 1000)
account.deposit(500)
account.withdraw(200)
print(account.get_balance()) # 1300

Final Tips for Mastering OOP in Python

  1. Always use self as the first parameter in instance methods
  2. Use __init__() to initialize attributes
  3. Practice with real-world entities like Car, Student, Book
  4. Apply concepts gradually — don’t force OOP where it’s not needed
  5. Explore advanced OOP: multiple inheritance, super(), and @property

Where to Go Next?

Once you’re comfortable with classes and objects:

  • Build a small project like a to-do app or student management system
  • Explore design patterns (e.g., Singleton, Factory)
  • Learn OOP with GUI using tkinter or PyQt
  • Apply OOP in web frameworks like Flask and Django

Leave a Reply

Your email address will not be published. Required fields are marked *