Understanding Object Oriented Programming(OOP) with Python— Why Object , Classes and other basic Concepts?

Data Weekend by Debby Alen
5 min readAug 16, 2020

--

Won’t it be a great idea if you could write a bunch of codes that are reusable/recyclable especially when working with complex programs ?

This set of codes could be bent to fit whatever action you intend for them to execute. Interestingly Object Oriented Programming adheres to creating structured reusable patterns of code that reduce redundancy in development projects as opposed to procedural programming which focuses on explicit sequenced instructions.

This simply implies that Object Oriented Programming enables you write reusable codes to execute a function that somewhat or totally differs from that of the initial code based on inheriting certain characteristics from that code (object or class) . This in turn makes your code more readable, modifiable and easy to maintain with the help of basic concepts ; Object, Classes, inheritance, polymorphism, encapsulation, procedure, abstraction etc

FIRST! LETS DEFINE OBJECT ORIENTED PROGRAMMING (OOP)

Object Oriented programming given the alias “OOP” is a type of software design or computer program that allows programmers to define the data type of a data structure, the types of functions or operations that can be applied to that data structure.

Now lets dive into the concepts that make OOP tick

WHAT IS A CLASS ? : A class is a blueprint/framework created by a programmer for an object , this defines the set of attributes/ properties / features that are used to characterize any object that belongs to that class.

WHAT IS AN OBJECT ? : An object is an instance of a class meaning it is an example of a class. It consist of both data and procedures to manipulate the data within a data structure.

Let's use this logical example to solidify our understanding ; Say we want to create a man named Daniel but to create this man he needs to have a body/skeletal framework which will have certain attributes/features like name, age, nose, mouth, ear, eyes etc that enables him to perform or act in certain ways(character).

Now the man “daniel” is AN OBJECT, where as the body/skeletal framework that contains the attributes that enable him perform certain functions is called A CLASS. The image below shows a class called daniel_framework and an object called daniel.

From the picture above you must have noticed def __init__ this is called; THE CONSTRUCTOR METHOD used to initialize data , methods are defined within a class, the __init__ method is the first instance of a class.

The self parameter is referring to the objects(instance of a class) that are made based on the class. Defining the class (daniel_framework) does not create any object rather a pattern for the object “daniel” which can then be instantiated/defined later hence Creating the class only provides a blueprint for an object!!!

WHAT IS INHERITANCE ?: Remember I mentioned earlier that OOP enables you create reusable patterns well here's why — Inheritance represents relationships between different classes, it is when a class makes use of a code constructed within another class.

Let’s use this logical example to solidify our understanding ; Biologically we all have a parent be it mom or dad and as children we inherit certain attributes or features from our parents e.g nose, face, height etc

Classes called subclasses or child classes inherit methods and variables from base classes or parent classes.

Now due to the fact that the child class can inherit from its parent class, the child class can reuse the code of the parent class without having to rewrite the line of code again hence decreasing redundancy.

In summary inheritance enables a subclass(child class) to inherit all attributes or objects from another base class called the “parent class” making use of the “Super Function” this function enables the child class to extract or inherit a property from a parent class .

WHAT IS POLYMORPHISM ? : Like the meaning of the word implies, “polymorphism” enables objects to take/have different forms, it is also the ability of a child class to inherit some(not all) objects from a parent class so as to perform a different function. Polymorphism allows room for flexibility so that your code can be extended and easily maintained over time.

In the code above, both the boy_scout and girl_scout class have two methods with the same name in common. However, each of the functionalities of these methods differ for each class.

In summary polymorphism gives the programming language the ability to process objects differently depending on their data type or class. It allows a particular object belonging to a particular class to be used in the same way as if it were a different object belonging to a different class.

WHAT IS ENCAPSULATION ? : I want you to imagine a drug capsule containing different formulations, that’s how encapsulation works , it acts as a combining process of different elements (class variables, instance variables, classes, objects etc) to create a new object.

WHAT IS A PROCEDURE ? : A procedure in object oriented programming is simply a section of a program that performs a specific task think of it as a function or set of functions, it is a type of encapsulation because it combines different series of computer instructions to execute a specific task.

WHAT IS ABSTRACTION ? : This is simply the process of extracting or selecting common attributes/features/properties of objects and procedures.

There are other basic concepts not covered in this article e.g, Information hiding, Messaging, Interface etc feel free to check them up to have more grasp of the concepts.

IN CONCLUSION

Object oriented programming principal advantage is the fact that it enables programmers to create modules that do not need to be changed when a new type of object is added, this enables the creation of objects that can inherit as many attributes/features from existing objects making object oriented programs easier to modify.

--

--

Data Weekend by Debby Alen

Passionate about building data-driven solutions, I write about Data analysis concepts & techniques , Tech industry trends and How to navigate career and growth.