Object oriented programming
Object-oriented programming (OOP) is a paradigm present in almost all modern programming languages. It is a way of thinking and structuring code that introduces the following concepts:
- ποΈ Class: These are the blueprints that allow you to create objects with specific characteristics. Like a cookie mold. They are defined using the
class
keyword. - π§ Object: Instances of a class with unique properties.
- π§ Method: Functions that belong to a class, defined with
def
. - 𧬠Inheritance: Enables a class to inherit and extend the behavior of another class.
- π¦ Duck Typing: Allows different classes to be treated the same way, as long as they implement the necessary methods.
Let us explore these concepts and see how you can apply them in your Python programs.
Here is a class: the Person
class.
class Person:
def __init__(self, first_name, last_name, age):
self.first_name = first_name
self.last_name = last_name
self.age = age
def is_adult(self):
return self.age >= 18
Here is an object: the p1
object of the Person
class.
p1 = Person("Ana", "Ruiz", 20)
This is a method: the is_adult
method.
p1.is_adult()
And these are the attributes: first_name
, last_name
, and age
.
print(p1.first_name) # Ana
print(p1.last_name) # Ruiz
print(p1.age) # 20
The concept of a class is generic. There can be multiple objects of the same class. We say that p1
and p2
are two objects of the Person
class.
- βοΈ They are similar in that they are defined by the same blueprint. Like cookies made with the same mold.
- π But they are different, as each has its own unique characteristics. A cookie made with the same mold but with different ingredients.
p1 = Person("Ana", "Ruiz", 20)
p2 = Person("Bob", "Lopez", 20)
Both have the same attributes but with different values.
print(p1.first_name) # Ana
print(p2.first_name) # Bob
Although object-oriented programming can become as complex as desired, these are the four fundamental concepts on which we will build the rest.
To better understand object-oriented programming and its advantages, let us write the above example without using it. Let us assume for a moment that we have no classes or objects.
The objective is the same: to store a personβs data and operate on it. For example, with a list, we can store the first name, last name, and age.
person = ["Juan", "Prieto", 23]
Now we can define the function is_adult
that receives a person
as an argument and tells us if they are an adult.
def is_adult(person):
return person[2] >= 18
At this point, we have something similar to the Person
class seen above, but we start to see some disadvantages:
- Accessing fields is not very readable. Using
person[0]
is less clear thanperson.first_name
. - There is no control over the length of the list. Someone could create a
person
list with more fields. Nothing prevents this. - The
is_adult
function is not related to the person. It is a separate function. - The variable
person
is accessible by anyone. Anyone could modify it without any restriction.
We have therefore already detected some of the problems that object-oriented programming can solve.
These are the most important concepts associated with object-oriented programming. Once you understand them, you will not want to go back:
- 𧬠Inheritance: Allows a class to inherit from another class, inheriting all its methods and attributes. A
Person
class can inherit from aHuman
class. - 𧩠Cohesion: Each class should contain related elements. You want
Person
to have attributes related to a person. It would not have the color of their house; that would be an attribute of another class,House
. - π Abstraction: Hides complex details from the outside. A TV remote is complex inside, but to a user, it shows only the volume and channel. It abstracts the complexity from the inside.
- π Polymorphism: In Python, this is related to duck typing. If two different classes have the same methods, they can be treated as the same.
- π Coupling: Measured as the degree of dependency between classes. Object-oriented programming helps reduce it.
- π¦ Encapsulation: Object-oriented programming allows you to hide internal details. The engine of your car is hidden. Hiding it minimizes the risk of someone tampering with it unknowingly.
Next, we will see in a practical way how to work in Python with object-oriented programming.