Stuck in a loophole with the basics of OOP in Python? Not after reading this blog.
If you are a newbie Python programmer, then you might be like this man who is banging his head on the wall because he is confused with OOP Programming. However, you don’t need to worry now because hopefully by the end of this blog, you will not only get familiar with OOP in Python but also be a ninja in OOP! I will go through the following topics:
- What is OOP Programming and how you can explain it to a five-year old?
2. How do you get started with OOP in Python?
3. What in the world are Methods, Objects, and Classes?
Without further a due, let’s get started with this blog post which will make you a ninja in at least the basics of OOP Programming.
What is OOP Programming really and how you can explain it to a five year old?
After continuously studying and researching about OOP Programming, I can confidently say that I can explain OOP to a five year old. But, why a five-year old? Well, because if you can break down a complex concept to a five year old, then you have successfully understood the topic. For a detailed explanation on why I use this approach, check out my blog post, “How to learn new concepts in a programming language?”
Here’s the Wikipedia definition:
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).
You might be confused with ‘paradigm’, ‘objects’, ‘data attributes’, and much more. However, how can you break down the complex definition into something that a five-year old can understand? Your answer should go something like this….
Say you have a car and a truck. Let’s assume the car’s color is red, has four seats, is smaller than the truck, and has a speed of approximately 65 mph. On the other hand, let’s assume the truck’s color is white, has 26 seats, is bigger than the car, and has a speed of 60 mph. Both the car and the truck are instances or types of vehicles that have the same properties or attributes(color, seats, size, and speed). However, they have different values for the properties, for example, the car has a speed of 65 mph and the truck has a speed of 60 mph. You can represent this in a chart below.
And BOOM! OOP explained to a five year old!
But, how does this example relate to OOP?
If you were to code this in an OOP setting, you will access the data attributes/properties of a Vehicle and create either a car or truck by changing the values of the properties.
However, you might ask that why do programmers even use this? The answer is that it allows your code to be organized and readable just like functions in Python. Plus, you don’t have to use the data attributes again and again for a specific property of a vehicle. You are able to use the same data attributes from a class and assign it different values based on the instance of the class.
If all of this didn’t make sense, then don’t worry! It will make sense once I demonstrate this in code form.
How do you get started with OOP in Python?
self.speed = speed,
self.size = size,
self.seats = seats,
self.color = colorcar = Vehicles(65,'small',4,'red')
QUICK NOTE: If I say “instances”, then I mean a type of the Vehicle class. As you saw in the chart, the car and truck are instances/types of Vehicles.
In OOP, it all starts with the magical word
class. After the word
class , you type in the name of the class .
Under class, you might have seen programmers create a function
def__init__(self,speed,size,seats,color) . But, what does this function even do?
def__init__() is a “constructor method” which assigns initial values to a class. Almost every class in Python has the constructor method. In the class “Vehicles” example, the function can be used to assign data attributes to the types/instances of Vehicles. The
self is the parameter passed inside the constructor method which refers to the instance or the type of the class
Vehicle . The other parameters
speed,size,seats,color are the data attributes or properties that the user assigns for an instance/type of class.
In order to create an instance, you declare a variable, call the class, and then set the values for the instance. For example, in your code, you created the instance
car and passed in the values for
Wait… What are you doing inside the constructor method??
The code block under the constructor method is simply setting the instance data attribute to the value passed into the method when the method is called. For example, if I create the variable
car and set it equal to
Vehicles(65,'small',4,'red') , then the values passed inside the parenthesis automatically becomes the data attribute of the instance car. You can visualize this when the variable
car is created:
self.speed = 65,
self.size = 'small',
self.seats = 4,
self.color = 'red'
NOTE: I put “small” and “red” in quotation marks since it’s a string.
If you want to print out a data attribute of an instance, simply do
print(instance.data_attribute) . For example, if you wanted to print out the color of the car, you would do
So far you learned how to create classes and instance. But, another question might come to your head as you learn and research about OOP……
4. What in the world are Objects, Methods, and Classes?
When you are doing OOP, you might encounter the terms Objects, Methods, and Classes. Going back to the example of vehicles, each term can be explained in detail.
Not these objects!
Some people tend to think objects and instances are interchangeable terms. They are, but there is a slight difference between the two. Object is the memory location of where the instance of the class is stored. For example, when you created the variable
car , an object was created and the name which refers to the object is the instance. Therefore, it’s preferred to say that you created an instance
car not an object
Before you start reading about methods, I would like to point out the difference between methods and functions since it tends to confuse some people. Methods and functions are the same but with different names. Methods are procedures or functions used in an OOP setting. Therefore, if someone says methods or functions, then they mean the same thing. However, calling functions “methods” is generally preferred when talking about OOP.
Just like functions, methods tend to interact with instances of a class in order to change its values. To give you an example, suppose you wanted to add 8 mph to the speed of your car instance. In order to achieve this, you can create a function called
add_speed inside the
self.speed = speed,
self.size = size,
self.seats = seats,
self.color = colordef add_speed(self,speed):
self.speed = self.speed + speedcar = Vehicles(65,'small',4,'red')car.add_speed(8)
add_speed acts as a method for the instance of the ‘Vehicle’ class when the method is called in the program. When you call the
add_speed method, the command
self.speed = self.speed + speed adds 8 to the original speed of the instance car. If you were to print out the car speed now, it would give you 73.
Nope you are wrong again! Not this class!
Simply put, “Classes” in OOP are simply a collection of variables and functions. For example, the constructor method, the
add_speed() function, and the instances are what created the class
Honestly speaking, NO!!
There’s a lot more to learn about OOP than just methods, objects, and classes. The journey of a true programmer doesn’t just stop at the basics. There are concepts like static methods, class methods, dunder methods, and much more!
However, if you followed till the end of this blog, I would say that now you are very well equipped with the basics of OOP.
If you like my explanation and would like me to explain complex concepts like these, then be sure to give it a clap and leave a feedback :)