Python Dictionaries(advanced)

Introduction Reading Time: 10 min

Table of Contents

Description

Class: A blueprint for creating objects (a collection of attributes and methods).
Object: An instance of a class containing real data.
Interface (Protocol): A way to define behavior contracts. In Python, this is typically done using abstract base classes or protocols (since Python doesn't have explicit interfaces like Java/C#).

Prerequisites

  • Functions and data types
  • Understanding self and constructors
  • OOP Concepts: Encapsulation, Inheritance, Polymorphism (optional but helpful)

Examples

Here's a simple program in Python:

✅ Defining a Class and Creating an Object
# Define a class named Person
class Person:
    # Constructor to initialize attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # Method to display person's info
    def display(self):
        print(f"Name: {self.name}, Age: {self.age}")

# Create an object (instance) of Person
p1 = Person("Alice", 25)
p1.display()  # Output: Name: Alice, Age: 25
✅ Using Inheritance
# Base class
class Animal:
    def speak(self):
        print("Animal speaks")

# Derived class inheriting from Animal
class Dog(Animal):
    def speak(self):
        print("Dog barks")

# Create object of Dog and call speak()
d = Dog()
d.speak()  # Output: Dog barks
✅ Interface using Abstract Base Class
from abc import ABC, abstractmethod

# Abstract class as interface
class Vehicle(ABC):
    @abstractmethod
    def start_engine(self):
        pass

# Concrete class implementing interface
class Car(Vehicle):
    def start_engine(self):
        print("Car engine started")

# Cannot instantiate Vehicle directly
# v = Vehicle()  # This will raise an error

c = Car()
c.start_engine()  # Output: Car engine started
✅ Interface using Protocol (Python 3.8+)
from typing import Protocol

# Define a protocol
class Flyer(Protocol):
    def fly(self) -> None:
        ...

# A class implementing the Flyer protocol
class Bird:
    def fly(self):
        print("Bird is flying")

b = Bird()
b.fly()  # Output: Bird is flying

      

Real-World Applications

Building software using Object-Oriented Programming

Modeling real-world entities like Bank, Vehicle, User, etc.

Used in frameworks and libraries like Django (Models, Views)

Interfaces are useful in plugin systems, APIs, or dependency injection

Where topic Can Be Applied

Web development (models, services)

Game development (characters, behaviors)

APIs and SDKs (standardizing behavior across components)

Enterprise software: Object modeling and data handling

Machine Learning: Creating reusable model wrappers, pipelines

Resources

Topic video source

A comprehensive video

Watch

Python pdf

pdf on topic

Visit

Interview Questions

What is the difference between a class and an object?

What is the use of self in Python classes?

How does inheritance work in Python?

What are abstract base classes? How are they used?

Does Python support interfaces like Java?

What is a protocol in Python typing?