Python OOP For Beginners - What Is OOP?


Here we are, about to tackle the final boss of the Python beginner’s journey. I haven’t met a beginner who isn’t afraid of this concept—not a single one, including myself back then.
I believe one major reason Python OOP becomes a nightmare for beginners is its name. It sounds scary—It did for me. And it’s heavily overrated as something hard to learn.
To be honest, it is a little harder compared to previous concepts beginners learn. But it’s not as hard as you might think.
I’m writing a series of articles on Python OOP, containing very short articles that are easy for you to read and easy for me to write. At the end of the day, we will truly beat this final boss.
Procedural Programming
Another major reason you may struggle to understand OOP is because you don’t know its opposite: the normal method which called procedural programming.
But believe me, you’ve already been doing procedural programming. In procedural programming, you give a sequence of instructions (procedures) to perform a certain task. To this point, almost every program you have coded can be categorized as procedural.
Imagine you have coded a program to calculate the area of a rectangle.
# Get length & width as inputs
length = float(input("Enter the length: "))
width = float(input("Enter the width: "))
# Define function to calculate area
def calculate_area(length, width):
return length * width
# Call the function
area = calculate_area(length, width)
# Display the result
print("Area of the rectangle is:", area)
It's clearly a sequence of tasks. It runs step by step from top to bottom. That's procedural programming.
Problems with Procedural Code
As long as you are coding small projects, it's okay to stick to procedural programming. But when you start creating serious projects using procedural programming, it’s only a matter of time before your code becomes messy, with too many repeated logics.
It becomes really hard to debug, read, and maintain.
As a beginner, you might not fully understand what I’m saying until you get real experience with OOP. But you will soon, in this article series.
What Is OOP
You probably know the full form of OOP, which is Object-Oriented Programming. As I mentioned earlier, it sounds pretty scary—at least, it did to me.
This concept focuses on organizing code to overcome the problems of procedural programming by bundling data and behaviors (attributes & methods) into objects.
These are the key concepts in OOP:
Encapsulation
Inheritance
Abstraction
Polymorphism
Don’t get overwhelmed by the list. We will cover these concepts in future articles.
Procedural vs OOP
You probably haven’t yet got why we use OOP just by reading the above paragraphs. So let’s look at some examples to see where OOP outperforms procedural programming.
Here is a procedural code.
# Dog 1
dog1_name = "Buddy"
dog1_sound = "Woof"
# Dog 2
dog2_name = "Max"
dog2_sound = "Bark"
def speak(name,sound):
print(f"{name} says {sound}")
def run(name):
print(f"{name} is running!")
# Dog 01
speak(dog1_name,dog1_sound)
run(dog1_name)
print()
# Dog 02
speak(dog2_name,dog2_sound)
run(dog2_name)
What are the cons here? This looks completely fine, doesn’t it?
Yes. As long as your program stays small, you won’t even notice the problems. So, let’s imagine scaling this program.
There is already one problem here: data and behaviors aren’t grouped together. If they were, we wouldn’t have to pass the same arguments like name
and sound
again and again.
Imagine we have more functions (behaviors) like eat
, sleep
, play
, and bath
. We would have to pass the same initial arguments repeatedly.
But we can find a solution to that in procedural programming. Look at this.
# Dog 1
dog1_name = "Buddy"
dog1_sound = "Woof"
# Dog 2
dog2_name = "Max"
dog2_sound = "Bark"
def dog1_speak():
print(f"{dog1_name} says {dog1_sound}")
def dog1_run():
print(f"{dog1_name} is running!")
def dog2_speak():
print(f"{dog2_name} says {dog2_sound}")
def dog2_run():
print(f"{dog2_name} is running!")
dog1_speak()
dog1_run()
print()
dog2_speak()
dog2_run()
Now, we don’t have to pass the same arguments repeatedly. But we have to create the same logic again and again, which is worse than the previous method.
Just imagine if we have ten dogs and more behaviors like eat
, sleep
, and play
. My God! Do the math. We would have to create fifty functions. What a waste!
So, do we have a solution?
Absolutely. That's the point of this article series.
Final Thoughts
Just like you are doing right now, I consumed a lot of information to learn this concept. I watched hours of videos, read ton of programming article and went through many books that focus on Python OOP.
And I’m not ashamed to tell you that this is not the smartest way to learn—not just Python OOP, but almost anything, especially if it’s related to engineering.
The best way to learn is by doing.
You should learn the basics and use them to build a ton of small projects. It doesn’t matter whether those projects are useful or already exist. The goal is to understand the concepts and master them so you will be able to build really useful projects in the future.
You can subscribe to the newsletter to get new articles delivered straight to your inbox the moment I post them. Follow me on GitHub for more contents— and maybe Instagram too.
You can support my effort by reacting to and commenting on this article. Share it with someone who would find this article valuable.
Subscribe to my newsletter
Read articles from Beenuka Hettiarachchi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
