Course

Python OOP: Usage, and Examples

Object-oriented programming (OOP) in Python is a programming paradigm that organizes code into reusable objects. Python supports OOP principles such as encapsulation, inheritance, and polymorphism, making it an efficient language for structuring complex applications.


How to Use OOP in Python

Python allows you to define classes and create objects that interact with each other. A class serves as a blueprint for creating objects, while an object is an instance of a class.

Defining a Class and Creating an Object

python
class Car: def __init__(self, brand, model): self.brand = brand self.model = model def display_info(self): return f"{self.brand} {self.model}" # Creating an object my_car = Car("Tesla", "Model S") print(my_car.display_info()) # Output: Tesla Model S
  • __init__ is the constructor method that initializes an object’s attributes.
  • self refers to the instance of the class.
  • Methods like display_info() define behaviors of the object.

When to Use OOP in Python

OOP is useful when designing applications that require reusable, modular, and maintainable code.

1. Encapsulation for Data Protection

Encapsulation restricts direct access to an object’s attributes. This prevents accidental modification and ensures data integrity.

python
class BankAccount: def __init__(self, balance): self.__balance = balance # Private attribute def deposit(self, amount): self.__balance += amount def get_balance(self): return self.__balance account = BankAccount(1000) account.deposit(500) print(account.get_balance()) # Output: 1500

2. Inheritance for Code Reusability

Inheritance allows a new class to inherit methods and properties from an existing class. This reduces redundancy in code.

python
class Animal: def speak(self): return "Some sound" class Dog(Animal): def speak(self): return "Bark" dog = Dog() print(dog.speak()) # Output: Bark

3. Polymorphism for Flexibility

Polymorphism enables different classes to define methods with the same name but different behaviors.

python
class Bird: def sound(self): return "Chirp" class Cat: def sound(self): return "Meow" animals = [Bird(), Cat()] for animal in animals: print(animal.sound())

Examples of OOP in Python

Example 1: Object-Oriented Design for an E-commerce System

python
class Product: def __init__(self, name, price): self.name = name self.price = price def display_product(self): return f"Product: {self.name}, Price: ${self.price}" class Clothing(Product): def __init__(self, name, price, size): super().__init__(name, price) self.size = size tshirt = Clothing("T-Shirt", 19.99, "M") print(tshirt.display_product()) # Output: Product: T-Shirt, Price: $19.99

Example 2: Using OOP for a Library Management System

python
class Book: def __init__(self, title, author): self.title = title self.author = author def get_details(self): return f"{self.title} by {self.author}" class Library: def __init__(self): self.books = [] def add_book(self, book): self.books.append(book) def list_books(self): for book in self.books: print(book.get_details()) library = Library() book1 = Book("1984", "George Orwell") book2 = Book("Brave New World", "Aldous Huxley") library.add_book(book1) library.add_book(book2) library.list_books()

Example 3: Polymorphism in OOP

python
class Shape: def area(self): pass class Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side ** 2 class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * self.radius ** 2 shapes = [Square(4), Circle(3)] for shape in shapes: print(shape.area()) # Output: 16, 28.26

Learn More About OOP in Python

Is Python an OOP Language?

Python supports OOP but is not strictly object-oriented. It allows procedural, functional, and OOP styles, giving developers flexibility.

OOP Concepts in Python

  • Encapsulation: Restricting access to data inside a class
  • Inheritance: Allowing one class to derive attributes from another
  • Polymorphism: Using a common interface for different class methods
  • Abstraction: Hiding implementation details from users

OOP in Python Examples

Real-world applications of OOP include game development, data modeling, and GUI applications.

Python OOP makes it easier to build scalable and maintainable programs, helping developers structure their code efficiently.