Object-Oriented Programming

Let’s make it easy for you to understand

Kafaru simmie
DataDrivenInvestor

--

Source: Pixabay

If you’re reading this, chances are Object-Oriented Programming is a concept that seems to elude you. If you’re a data professional using Python, this is a concept you’d need to familiarise yourself with.
I intend to make this as short as possible, using a simple analogy and some Python code to help make this clearer. If you’re looking for some corporate definition of OOP, this might not be a great resource for picking up a bogus definition. Rather, this is just an explanatory article, with an intent to keep this as simple as possible.

The Analogy

Take for example the codes below that creates a list and then removes an element from the list

We’ve been able to successfully remove Blue from the list created, but let's attempt to do the same for a tuple.

We get an error that says, ‘tuple’ object has no attribute ‘remove’. Of course, we all know that tuples are immutable, right? This means fundamentally tuples have no function that will alter their structure.

This concept where we define data structures and also associate functions (or methods that can allow you to manipulate them is called OOP). It still doesn’t make sense right?

Now imagine a Lioness and then imagine the Lioness gives birth to a cub. Now you’d agree with me that anything that comes out of this Lioness will ALWAYS be a Lion. This implies that the Lioness is a Template, where whatever is built after that Template will always be like the Template. The number of cubs given birth doesn’t diminish their characteristics as Lions, because they always are birthed after the template (The Lions Genes).

Simply put, whatever comes out of this Lioness will always have the Genes of a Lion.

Take a minute to digest the above…

In python, every time you create a list (The template) with square brackets, it always has the characteristics or Genes associated with lists which include mutability, index, some functions like; .remove, .append, etc.

Same with Tuples, anything created after the order of the tuples will have Genes such as immutability, .count() etc.

Associating the Above with OOP Concepts

There are some terms we will break down in OOP and associate them back with the Analogy above;

  1. Classes: This is the Original Template, which all the other cubs will inherit its Genes. If you create a Pandas Dataframe it will have the Genes or Characteristics of a Pandas DataFrame, if you create a NumPy object it will always have the genes of a NumPy object. Simply put, Lions are Classes of their own, same with Tigers, Cheetahs etc. All these are classes of their own because they have different features as defined by evolution.
  2. Objects: These are creations or products or birthings of a Class. So a cub is an object of the Class Lion. A Pandas Dataframe is an object of the Class Pandas Dataframe, a List is a data structure from the Class List, a Dictionary is an object from the Class Dictionary. Therefore, any object that emerges from a Class will ALWAYS have the Genes (the possibilities available in that Class). Similar to how Male Lions will always have their fur or Python Lists will always be mutable.
  3. Methods: These are the abilities provided to any object of a Class. This is particularly why you have the dot (.) in python.

Methods are simply functions, but functions available to ONLY a particular class.

Think of it this way, earlier I showed you an image of where I created a list and then removed an item from the list using the (.remove) function. Why isn’t that available to Tuples? Think about it for a minute….

Because Tuples do not have Genes or Characteristics that allow you to change them. Similar to how Birds (which are classes of their own) can fly and Lions (which are classes of their own) can’t.

So every Python Object belongs to a class and the class they belong to will determine the kind of operations you can execute on them. In the image below, I try to reset the index on a Pandas Dataframe and as well a NumPy object.

Notice how the error is similar to what I had earlier on the tuple. Because Pandas objects have resettable indexes and NumPy objects do not.

4. Attributes: These are the features, the things that identify an object to a Class. How do you know this Lion is a Lion? How do you know a Pandas Dataframe when you see one? How do you Identify a List? Attributes are definable characteristics of an Object of a Class. Similar to the above image from the methods, we can see indexes on a Pandas Dataframe but we don’t have the same on a NumPy object.

Check the image below out;

Creating a Sample Class

In the code below, I create a Class Called Lion. The Attributes (Identifiable features) of the Lion class include; Color and gender. The function called Show is the method applicable to this class.

class Lion():
def __init__(self, color, gender): #The attributes
self.color = color
self.gender = gender
def show(self): #The method/function associated to this Class
print("The color of this Lion is: ", self.color)
print("The Gender of this Lion is: ", self.gender)
return

Conclusion

We’ve explored in simple terms what OOP is all about, let me know in the responses if you have any questions or any part of the post you think can be optimized.

--

--