I remember a few years back when I first started my development journey, I went through an online Python and Django course.
A year later after developing a couple of portfolio projects, I thought I was ready to get a job, so I started applying for python backend developer jobs.
A week before my interview, I started cleaning up my code and then began practicing Django related interview questions.
The night before my interview, I called a friend of mine(A Senior Developer) to go over what all I did and get any tips from him for the interview.
He asked me if I was good with OOP and I told him sure, using classes?. He then asked me If I knew what “Polymorphism” was. I then asked, “What’s that? lol”. I obviously didn’t have a good understanding of it and I thought it was using classes in general. He then advised me to go back home and look up all the OOP concepts.
The next morning, most of my interview questions were about OOP and I only had a couple of questions about SQL.(I got the job luckily)
I then decided to write an article about Python’s OOP concepts in case someone needs it all in one place.
Throughout this article, I will be using the simple better than complex and the show by example approach.
As you probably know, Python is a dynamically typed language and the implementation of OOP in it is somewhat different than other languages that use OOP also e.g Java.
I’ve been using Python for like 3 years now and I didn’t really have to use most of it because of the type of work I did which relied much on frameworks and not on pure python but now since I’m working on an IaC team writing pure python engines, I’m taking advantage of those concepts to make my and my team’s lives easier.
Definition: A class is the “Blueprint” template that contains “Attributes(variables)” and “Methods(functions)” that will take data as ‘Input” and handle it using the specific rules(Methods) you defined then gives you output or perform certain jobs.
The pros of classes are,
- Prevents breaking the #1 rule in programming which is: Do Not Repeat (DRY) yourself.
- Better readability.
- Better maintainability.
- Easier manipulation and extensions.
- It uses less memory.
- Creates models of real-life-like objects.
A simple basic example of classes,
Definition: Objects are instances of a class so using the above example, doorOne is an instance of Door.
Definition: Inheritance is what it literally means. It’s a CHILD taking/Inheriting from his PARENT his properties and behaviors. The technical terms used in that matter are also called
- A Parent class
- A Child class
A simple basic example of classes inheritance,
Big word, Yea?
Let’s break it down first before we actually define it.
Poly means many and morph means forms(I think in greek). So If we combine them, It will mean many forms. What would that even mean, You asked?
Okie, Let’s use the above example in inheritance. We have a method called Area, Yea? That calculates for us the Area of the Door which is a rectangle shape. What if we have a special shape like an octagon and calculating the area uses different calculations, what do we do now?
That’s where Polymorphism plays a part, It takes a method from the parent class and rewrites it (change its form), that’s it. really !!
I don’t know what they make stuff sounds so complicated.
Definition: It’s the ability of a method/function/object to take a different form.
A simple basic example of classes Polymorphism,
Definition: It’s creating a class that can’t be modified by any other functions/classes, Etc, or by the user. It’s used to protect against hacking attacks. All of its methods and algorithms must be predefined INSIDE the class. Let’s see an example
A simple basic example of classes Encapsulation,
It’s best shown by example so let’s get to it after defining it.
Definition: It’s also as it sounds. It’s having 2 classes or more and one of the classes is a component with the other class.
NOTE: This is different from Inheritance.
A simple basic example of classes composition,
Definition: Aggregation is very similar to composition where you can use the objects of a class in another class but always one class is dependent on the other.
Let’s see an example and explain it afterward.
A simple basic example of classes Aggregation,
Definition: It’s pretty much a diverse relationship between 2 classes or I’d put it as a ManyToMany relationship as it’s called in Django. Each class can access the other’s objects pretty much.
A simple basic example of classes association,
Definition: It’s showing a feature of an object without showing the technical details or methods behind it. This is something you do as a programmer every day, You write code and you make an application but the user doesn’t see the code. He only uses the features of the code that you wrote for him. That’s abstraction in a nutshell. In OOP, it’s creating a class that will have certain functionality and if a user wants to use it, They have to follow your model of using it.
A simple basic example of classes abstraction,