This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here,
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
- 30 Days Of Python 👨💻 - Day 8 - OOP Basics
The first language that I was taught in school was BlueJ back when I was in fifth grade. It is a Java Development Environment designed for beginners and it was part of our school curriculum. That was my first introduction to Object-Oriented Programming. Although I didn’t understand much during those days, I still have vivid memories of our computer teacher explaining the pillars of OOP. I recollected some of our teacher’s analogies while exploring the principles of OOP in Python today and have tried to fuse them with some of my own.
There are basically 4 pillars or core principles of OOPs or so-called the pillars of OOP.
Encapsulation
While covering the basics of OOP in my previous post, I already covered about Encapsulation indirectly. Encapsulation in simple terms means to create a container where attributes and actions associated with those attributes are grouped together.
- class Avenger:
- def __init__(self, name, knownAs):
- self.name = name
- self.knownAs = knownAs
- def reveal_identity(self):
- print(f'I am {self.name}, also known as {self.knownAs}')
- hulk = Avenger('Bruce Banner', 'Hulk')
- iron_man = Avenger('Tony Stark', 'IronMan')
- hulk.reveal_identity() # I am Bruce Banner, also known as Hulk
- iron_man.reveal_identity() # I am Tony Stark, also known as IronMan
In the above class, the identity of each Avenger is revealed, when the reveal_identity method is called. In other words, their identity has been encapsulated. These methods which perform some action makes classes so powerful. While creating objects from the class, we can just call these methods to perform actions without bothering about how they do those actions. Just like Avengers!
Abstraction
To learn to drive a car, we need knowledge of just 3 things - A, B and C which is the accelerator, brake and clutch (Now with automatic cars, it even simpler). As drivers, we don’t need to know what makes the car accelerate or how the car comes to a halt when applying brakes. This is called abstraction. It is everywhere in real-life. A gentle press on a power button starts up a computer, a tap on the screen captures a photograph from our mobile etc. In the above example, the reveal_identity method is abstracted.
- class Avenger:
- def __init__(self, name, knownAs):
- self.name = name
- self.knownAs = knownAs
- def reveal_identity(self):
- print(f'I am {self.name}, also known as {self.knownAs}')
- hulk = Avenger('Bruce Banner', 'Hulk')
- hulk.name = 'Thanos'
- hulk.knownAs = 'Loki'
- hulk.reveal_identity() # I am Thanos, also known as Loki
There is a problem with the class implementation above. Although a hulk was created, Loki came and decided to camouflage hulk into Thanos! This is not what I want so right now although there are encapsulation and abstraction implemented, the internal functionality can be tampered or modified from outside.
Python does not support the creation of private variables by default. Will have to explore more to see if it is possible to make variables and methods inaccessible and gain true abstraction.
There is a convention however to create a private variable by prefixing it with _. This allows other developers to recognize it as a private variable and not accidentally try to update those variables and methods( though they can). This is very similar to how we define a private variable in the JavaScript universe.
Inheritance
Inheritance as the name suggests means to inherit properties and functionalities from a parent. Based on a parent class, any number of sub-classes can be created that inherit from the parent class. This is done to share functionality among similar classes without writing the same logic again and again.

Join the conversation! Your thoughts help the community grow.