Machine Learning
Data Analytics

Python’s OOP Revolution

Sumer Pasha

Sumer Pasha

Dec 18th - 1 min read

Welcome to the world of Python, where simplicity meets power. we embark on a journey into one of the core pillars of Python programming - Object-Oriented Programming (OOP). OOP is a programming approach that mirrors real-world entities and their interactions, making it a powerful tool for structuring code. Let's unravel the intricacies of Python's OOP concepts.

Think of OOP as a lens through which you view the real world. Imagine objects like cars, houses, and even people. Each has its own unique characteristics (attributes) and capabilities (behaviours). Similarly, in the digital realm, objects are created using classes. These blueprints define the attributes and behaviours that all instances of the class will inherit.
The Building Blocks of OOP

  • Classes

The blueprints that define objects. They contain attributes (data) and methods (functions) that define an object’s behaviour. The prototype or blueprints from which things are made are contained in a class. It is a logical entity with certain characteristics and functions.

Some points on Python class:

  1. Classes are created by keyword class.
  2. Attributes are the variables that belong to a class.
  3. Attributes are always public and can be accessed using the dot (.) operator.
  • Objects

Instances of a class, with their own set of attributes and methods. Think of them as specific cars, houses, or people, each with unique characteristics. The object is an entity that has a state and behaviour associated with it. Any physical thing, such as a mouse, keyboard, chair, table, pen, etc., might be it.Integers, strings, floating-point numbers, even arrays, and dictionaries, are all objects.

  • Attributes

The data that defines an object's state. These are like the color of a car, the number of bedrooms in a house, or the age of a person.

  • Self

The data that defines an object's state. These are like the color of a car, the number of bedrooms in a house, or the age of a person. In Python, the instance of the class that is now being used is referred to as the "self" when working with classes. "self" is typically used as the first parameter in class instance methods. Whenever you call a method of an object created from a class, the object is automatically passed as the first argument using the “self” parameter.

  • init

The method is useful to do any initialisation you want to do with your object. 1 (1).png

Example: -

We defined a Dog class with a constructor (init) to initialise its attributes (name and age). The bark method represents the behaviour of the Dog class.


Letting new classes take on properties and functions from older ones. Consider how a sports vehicle adds its own distinct characteristics and behaviour to the qualities of a regular car. One class's capacity to obtain or inherit properties from another class is known as inheritance. The class from which properties are derived is referred to as the base class or parent class, while the class that derives properties is referred to as the derived class or child class.

Types of Inheritance
Single Inheritance:
Single-level inheritance enables a derived class to inherit characteristics from a single-parent class.

Multilevel Inheritance:
Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class.

Hierarchical Inheritance:
Hierarchical-level inheritance enables more than one derived class to inherit properties from a parent class.

Multiple Inheritance:
Multiple-level inheritance enables one derived class to inherit properties from more than one base class 2.png

In the above code Cat and Dog classes inherit from the base class Animal. They override the abstract speak method to provide their own implementation.


It is possible to consider objects of various classes as belonging to the same base class thanks to polymorphism. This enhances flexibility and modularity. Polymorphism in Python refers to the ability of objects to take on multiple forms. In the context of object-oriented programming, polymorphism allows objects of different classes to be treated as objects of a common base class. This can be achieved through method overriding and the use of a common interface. Allowing objects to respond differently to the same message.



The animal_sound function takes any object that has a speech method (polymorphism). It allows us to treat both Cat and Dog objects as if they were of the same type.


The idea of encapsulation is to combine data and methods that work with the data into a single unit, or class. This protects the internal details of the object. Encapsulation in Python is a fundamental concept in object-oriented programming that involves bundling the data (attributes) and methods that operate on the data into a single unit known as a class. It restricts access to some of the object's components, providing data protection and implementation hiding. Protecting an object's data by bundling it together with its methods


the balance attribute is protected by using a single underscore (_). External access to _balance is discouraged, but it's not strictly prohibited.

Advantages of OOP’s

  1. Modular Code: Divide your code into smaller, well-defined units (classes) for improved organization and maintainability.
  2. Code Reusability: Share code across different objects and applications through inheritance, reducing redundancy and development time.
  3. Scalability: Build complex systems with ease by creating new classes that inherit and extend existing functionality.
  4. Maintainability: Simplify bug fixes and updates by isolating code in specific classes, reducing the risk of unintended side effects.


Python's Object-Oriented Programming concepts provide a powerful and flexible way to structure code. By understanding and leveraging these concepts, you can create well-organized, modular, and reusable code. Whether you're a beginner or an experienced developer, incorporating OOP principles into your Python programming will undoubtedly elevate your coding skills to new heights. Happy coding!

about the author

Sumer Pasha is a Digital Automation Engineer with Analogica India. He is a python developer and uses python to develop internal utilities for Analogica.