Skip to main content

Command Palette

Search for a command to run...

Introduction to Object Oriented Programming in Python

Updated
5 min readView as Markdown
Introduction to Object Oriented Programming in Python
U

I love to do two things: demystify tech and build tech communities.

You might have heard the word 'Object Oriented Programming' or OOP before now and have wondered what it is. It is a beautiful programming paradigm that helps make your work as a developer smooth. Let's go on a ride through OOP-land but before then, let me tell you a story.

Two of your friends, Tricia and Jim just visited your house to catch up because you haven't seen yourselves since you left college ten years ago. When they landed at your house, there was no meal at home because it was an impromptu visit. So you ask them what they would love to eat and they say, "Basmati Rice". You take the following procedures:

  1. Walk to the next grocery store to buy a pack of basmati rice.

  2. Walk to the farmers' market to buy some fresh vegetables and other ingredients.

  3. Return home, cut and prepare the ingredients.

  4. Cook the rice and add all the ingredients

  5. Serve your guests.

Easy-peazy, right? Your food was so delicious and Tricia and Jim ate this food like their lives depended on it. It was so good that they asked if you would be willing to cook this same delicacy for the 1,000 guests at their upcoming wedding next month. Out of joy, you agree to cook at their wedding. But then, you notice something: though the end product of your 'wedding catering services' is still the delightfully-tasty Basmati rice, you cannot use the same process above to prepare it. First, you cannot just walk into a grocery store to buy basmati rice for 1,000 people. No grocery store in your area has such quantity on-hand. This implies that you have to get a procurement manager, whose role is to go round all the stores to buy basmati rice, till they get enough for the wedding. They will also be responsible for buying the vegetables and ensuring all are delivered to the kitchen. Also, because of the sheer size of the food you are preparing, you need extra hands: cooks who will help with the preparation and cutting of ingredients, mixing and other activities required. You act as the chief caterer/project manager supervising all activities aimed at delivering a tasty culinary experience at your friends' wedding.

Did you notice something? The steps you took to cook the basmati rice when it was just Tricia and Jim were straightforward: a set of five procedures, beginning at procurement to serving the food (this is known in programming parlance as procedural programming). But the moment you had to scale and prepare food for 1,000 guests, you needed another paradigm. This is the whole gist about OOP.

When you are building small stuff, procedural programming works. Procedural programming is a programming paradigm that builds stuff just using a set of procedures. But procedural programming never scales well. When it is time to scale and do the big catering, you see that you need a new paradigm. This paradigm demands that you get 'OBJECTS' (like the procurement manager, cooks, etc) to handle various parts of the project. With this, if you are informed that the guest list has been updated to 2,000 guests, all you need to do is to add maybe one more procurement manager, two more cooks and you'll be fine. It's not this easy if you were still doing everything yourself using procedural programming. At this juncture, I must inform you that OOP is applicable anytime you have a task to perform, whether it's cooking or a computing task like machine learning.

I think we can call it a day at this point (Ok! Kidding!! I hear you asking, 'Where are all the Python codes??'). Before we delve into coding in Python, let's do some housekeeping so you can understand the possible scenarios that you'll encounter in Object Oriented Programming (OOP).

Still using the catering example above, you have to take a series of steps to ensure your catering project is successful. First, you need to hire. From your earlier assessment, you have seen that you need three classes of individuals to work on this catering project:

  1. Procurement Manager

  2. Cook

  3. Supervisor (which is you!)

This is exactly the first thing you do in OOP: you identify the classes (of helpers) you would need to execute the tasks in this project. In order to do this, you segregate the tasks into clusters and assign homogenous tasks to a particular class (more on this later!)

Next, like every good human resource manager, you have to do two things: identify the FUNCTIONS these classes will perform and state the QUALITIES the classes need to have in order to perform their tasks perfectly. In OOP lingo, the FUNCTIONS are called METHODS and the QUALITIES are called PROPERTIES. It is important you do this before you begin to hire (in real life, this is called 'job description' and 'job specification'. Rings a bell, right?)

Now that you have the job description and job specification ready, you need to put word out so that you can get candidates. And after a series of LinkedIn posts and ads, you hire Lenny as a first procurement manager. In OOP lingo, we say Lenny is an INSTANCE of the Procurement Manager class. If the guest list baloons, you might hire Jenny as another procurement manager. The class (Procurement Manager) has already determined what their tasks should look like and the qualities they should have. You have identified Aisha and Denise as instances for the Cook class, while you are the instance for the Supervisor class.

At this point, I think I still hear you asking me, "But where's the Python Code?". Ok. We'll get to it soon (in the next part of this post). I promise!!

If you like my posts and would love to support me or hire me for some freelance data consulting sessions, please use my Buy-Me-A-Coffee link (www.buymeacoffee.com/onyeka) or click on the Buy-Me-A-Coffee button at the top of this post.