Introduction to OOP

Introduction to OOP

OOP stands for Object-Oriented Programming; it is a programming paradigm (or concept) that is aimed at making our program more readable and structured by grouping together all of the related variables and functions into a class. A class provides a blueprint of related variables and functions, and we create an object or instance of the class which would have actual values.

Through OOP we can create a structure that is a model of real-time entities. Consider a program that takes a person’s height and weight to compute their BMI (Body Mass Index).

class BMI:  # Compute Body Mass Index

    def __init__(self, height, weight):
        # this is called when an instance of the class is created  
        self.height = height
        self.weight = weight

    def height_based_computations(self):
        # this function converts height to meter and the returns the square of the meter
        meter = self.height / 100
        meter_sq = meter * meter
        return meter_sq

    def calculate_BMI(self):
        # this function calculates your BMI
        required_height = self.height_based_computations()
        BMI = self.weight / required_height
        BMI = round(BMI, 1)  # rounds the BMI to one decimal place
        print("Your Body Mass Index is:", BMI)


# first instance
person1 = BMI(height=166, weight=52)
person1.calculate_BMI()

# second instance
person2 = BMI(height=170, weight=56)
person2.calculate_BMI()

As we execute this we should get:

The code for the above might seem overwhelming but it is quite simple. We created a class called “BMI” and defined 3 functions within it.

The first one is called __init__(), which is a function we can use to either create a variable or call a function each time an instance of the class is created. You can see that we created 2 variables with the name of the parameters but with a ‘self’ and a ‘.’ preceding it.

self.height = height
self.weight = weight

This means we are taking the value given as an argument, and storing it in a variable that can be used by the entire class. The ‘self’ refers to the value given as an argument for each instance. When we created an instance of this class, we gave 2 arguments:

# first instance
person1 = BMI(height=166, weight=52)

# second instance
person2 = BMI(height=170, weight=56)

Therefore, whenever we use the object or an instance of a class, ‘self’ will refer each to the values of a particular instance. In other words, ‘self’ tells Python that for “person1” use 166 & 52, and for “person2” use 170 & 56.

To calculate our BMI, we need to convert our height into centimeters and then square it so the second function does this exact operation, and then we must divide our weight by the above square value, which is done by the third function.

# first instance
person1.calculate_BMI()

# second instance
person2.calculate_BMI()

We also called the function to calculate the BMI of this instance, using the dot operator. That is, we are telling Python to calculate the BMI for the values stored in instance 1 (person1) and instance 2 (person2) respectively. Instantiating is a term that refers to creating of instances.

NOTE: Classes might seem conceptually similar to modules, but there are differences. A class offers a blueprint on which we build-up, whereas a module is a file that packs together similar variables, functions, and even classes.

With Object-Oriented Programming, the idea is to create or write reusable code. The underlying principles of OOP which make it so resourceful, include:

  • Inheritance: The ability to create new classes that have properties from another class, without having to modify the original class.
  • Encapsulation: The ability to conceal details that should be undisclosed.
  • Polymorphism: The ability to use functions in varying ways for various inputs.

While it might not make much sense now, all of the above provides simplistic ways to solve intricate problems, while creating a readable modular structure that promotes code readability. Inheritance allows us to build more complex programs coherently as we build on top of one another.

What have we learned?

  • What is the full form of OOP?
  • What is OOP?
  • What is a class?
  • What is the __init__() function for?
  • What is an instance?
  • How do we use ‘self’ in Python?
  • How does a class differ from a module?
  • What are Inheritance, Encapsulation, and Polymorphism?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments