Object-oriented Programming in Python 🐍🐍

Object-oriented Programming in Python 🐍🐍

Hi Everyone 👋

Today I wanted to share what I have learned about object-oriented programming. 🚀

Introduction

Object-oriented Programming (OOP) is a computer programming model that helps one organize their software design around data, class, and object rather than functions and logic. Just like you have Knowledge and can perform various methods, Objects have data and can perform various methods.

Class is the blueprint used to create a user-defined data structure. It holds the common attribute and behaviors of the object. For example, the class “Ride” will hold objects “car”, “plane” and “bike”.

Object is an instance of a class. It has both a property (variables) and behavior (methods). The python object “Car” will have properties; “make”, “model”, “four_wheels” and behaviors; “start”, “stop”.

Method is the behavioral factor of the object. Modifying a method in the class “Ride” would affect all instances of it; “car”, “plane”, and “bike”.

Instance is simply any object made from the class. An instance of the “Ride” class would contain real unique data. A class is like a form while an instance is a filled variation of that form.

It is good practice to identify all the objects you want to manipulate and how they relate among themselves. This is data modeling.

Principles of OOP

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

1. Encapsulation State, variables, and methods should be private unless they are being declared public.

An object “car” of a class “Ride” can manage itself via methods “start” and other class “House” cannot touch it expect allowed to. We can only use the methods to communicate with our object. If we want another class “House” to change the state of the “car” object, we have to create public methods “over_ride_and_start” to modify or invoke the start variable. This connection is encapsulation.

2. Abstraction Hide all implementation of the class from anything outside the class.

Our object should hide details and only expose relevant information to another object. Thus public method should be created so object can be called. The “Al voice recognition” may start our “car” object (in the “Ride” class) and open the “Lights” object (in the “Home” class) without having access to the details of our car. This makes maintenance of an extremely large database-less daunting and lowers the chances of bugs.

3. Inheritance Child class should have a mechanism that enables it to inherit behavior and properties from the parent class.

Reusability is the advantage here. Child class “car”, “boat” objects should acquire properties (“has_an_engine”, “can_be_used_for_transport”) from the parent class “Ride”. However, they should have their own unique properties.

4. Polymorphism Interfacing with objects and receiving different forms or results.

This allows us to implement a class exactly as intended for the parent class. This prevents errors in types and each sub-class keeps its own methods. Our class “Ride” has a speed method. Our child class; “Object”, “boat” and “plane” can utilize this method. However, they would each have their own iteration of the speed method.

OOP in Python 🐍

Here is a link to the notebook on my Github. You should download it and follow along.

First, we define our Class with the class keyword followed by the name of the class.

class Car:
    pass #This is also where subsequent code will eventually go.
car()

The pass acts as a placeholder and thus prevents python from returning an error. At the moment, our car class is without attributes or methods. Let's instantiate our object class by calling the class. Every instance of our class is stored in a new memory address. which means every instance of the Car class represents two different objects. Wanna try?

a = Car()
b = Car()
print (a == b)

Our class has a docstring attribute called doc which holds the documentation of the object.

class Car:
    #this is the docstring and it gives a brief about the class
    """ A four-wheeled road vehicle that is powered by an engine car has four wheels"""

Car.__doc__

Let’s make our class interesting by giving it attributes. There are two types; Class attributes and Instance attributes.

Every attribute in our class would be defined in a method called .init() with self as the first parameter. These are called instance attributes. However, class attributes are attributes that have the same value for all instances of that class. Hence it would be assigned outside the .init() method. All instances of car class (model and colour) are four-wheeled.

class Car:
    #Class attributes
    wheels = "four"
    def __init__(self, model, colour):
         self.model = model
         self.colour = colour

Let’s give our instance attributes parameters values. 🧨

Toyota = Car("Toyota", "yellow")
kia = Car ("Kia", "black")
Ford = Car("ford", "blue")
Honda = Car("Honda", "brown")

We have four Car instances with each having unique values. Let’s access the data in these attributes.

#instance attributes
Toyota.model
print(Toyota.model)

kia.colour
print(kia.colour)

#class attributes
Toyota.wheels
print(Toyota.wheels)

kia.wheels
print (kia.wheels)

Like I mentioned all instances in a class have .wheels, .model and .colour. However, the values of the instances attributes (.model and colour) would differ; encapsulation and polymorphism. We can also update and modify the values in our instance without changing the values.

#modifying our attributes
Toyota.colour = "red" #previously yellow
print (Toyota.colour)

kia.wheels = "two" #previously four
print (kia.wheels)

Let’s look at instance methods. Just like .init(), the first parameter is also always self and is defined in our class. Let’s create two instance methods; speed and details.

class Car:
    wheels = "four"
    def __init__(self, model, colour):
         self.model = model
         self.colour = colour

    # Instance method
    def details(self):
        return f"this {self.model} is {self.colour}"

    # Another instance method
    def speed(self, speed):
        return f"This {self.model} is {speed} at top speed"

Let's apply these new methods.

Toyota = Car("Toyota","yellow")
print (Toyota.details())

#Toyota.speed(280)
print (Toyota.speed(280))

Let’s look at inheritance. Let’s say we want to add a new class “engine”. We can’t always specific the engine for each car. Let’s create a child class for it

class Hybrid_engine(Car):
    pass

class Gas_engine(Car):
    pass

Now let’s instantiate these engines for the cars.

Toyota = Gas_engine("Toyota", "yellow")
kia = Gas_engine("kia", "black")
Honda = Gas_engine("Honda", "brown")
Ford = Hybrid_engine("Ford", "blue")

Thus, all objects created from the child class is an instance of the parent (car) but might not be in the same child class (Gas_engine and Hybrid_engine). Let’s give it the parent’s functionality specific to it (Inheritance)

class Hybrid_engine(Car):
    def speed(self, speed="210"):
        return f"{self.model} Speed; {speed}"

Ford = Hybrid_engine("Ford", "blue")
print (Ford.speed())

A change in the parent class wouldn’t affect the child class.

class Car:
    wheels = "four"
    def __init__(self, model, colour):
         self.model = model
         self.colour = colour

    # Instance method
    def details(self):
        return f"this {self.model} is {self.colour}"

    # Another instance method
    def speed(self, speed):
        return f"This {self.model} is {speed} at top speed"

Toyota = Gas_engine("Toyota","yellow")
print (Toyota.speed(367))

#however ford wouldn’t get the change since we define his child class

Ford = Hybrid_engine ("Ford", "blue")
print (Ford.speed(467))

However, if we didn’t want this to happen. We would need to use the .super built-in function for the child class. 🙃

class Hybrid_engine (Car):
    def speed(self, speed="210"):
        return super().speed(speed)

Ford = Hybrid_engine ("Ford", "blue")
print (Ford.speed())

Codes

You can find the codes here:

Summary 🎉

I hope you will find this post useful and learn something new in the process. If you have any comments, additions, or questions please let me know in the comment section.

Feedback, suggestions, and recommendations are highly welcome. This will really help me a lot and motivate me to share more content like this in the future also. 😀

If you have read this far, I really appreciate it!

Connect with me at Twitter | LinkedIn | Github

Happy Coding!