Classes and Object-Oriented Programming(OOP) in Python: A beginner’s Guide

Oluwabukunmi Ige
Analytics Vidhya
Published in
8 min readSep 22, 2020
Photo by Dakota Roos on Unsplash

Classes and OOP is a topic that took me quite a grasp, as such, I had to do a lot digging and extensive reading before I could gain some sort of high-level understanding of the concept; this prompted me to document what I have learnt so far and put out this blog post. The notion is that I want it to serve as a reference when I need to revise the concepts of OOP and also as a guide to beginners who are looking to gain a High-level understanding of Classes and OOP in Python.

For starters what is Object-Oriented Programming or OOP as it is mostly called? According to Wikipedia OOP is a programming paradigm based on the concept of “objects”, such that it organises software design based around objects in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.

OOP in Python employs Classes as it’s the main tool in a coding structure to implement new kind of objects that support inheritance(will be covering this shortly). An object can be defined as a data field in memory that has unique behaviours and OOP focuses on how you can manipulate these objects.

A high-Level explanation of OOP
So far we have covered the basics of OOP but this explanation might not be totally relatable, so let’s see if we can do better by explaining some of the important tenets of OOP using more relatable concepts.

Composition
So imagine the setup of a vehicle, from the engine to its gearbox and say it’s wheels. These are a collection of items working together as a unit to make up the vehicle. For the Vehicle work successfully the engine has to supply power, the wheels enables movement and so on. In OOP lingua this is known as composition and each different component can be coded as a method which will make up the Class of the vehicle.

Inheritance
For inheritance, let’s say our Vehicle is a truck, we are using vehicle because can serve as a generic term for what we are trying to explain. The Sub-class will start off as a Vehicle and the super-class will be that it’s a truck which builds on the features of the vehicle by adding more features. So it starts up a vehicle(which is general) and it inherits the properties of a vehicle in addition to its super-class of being a truck and the truck’s new properties. We can always use the sub-class of a vehicle to buildup other cars like lorries, SUVs et al. This property is known as inheritance and it is one of the main promoting features of OOP.
In a High-level Classes are Python Programming units that are compartments for packaging logic and data which a layer of the structure that supports better programming practices such as reusing and customizing code.

OOP Nomenclature
Before we dive into code, let’s explore some OOP nomenclature.

Object: An entity that stores data
Classes: Describes an object type by defining what data is stored in the object and what actions can the object perform. It also serves as a factory for generating instances
Instances: Describes a specific example of a class. Instances inherit attribute from its class. Each time a class is called, a new instance of that class is generated. Like for our different instances will be different colours of the Class truck.
Attribute: is a variable that belongs to an instance of a class
Method: is a function that belongs to an instance of a class and determines what action the object in the class can perform. The main difference between an attribute and a Method is that an attribute stores data and a method performs an action. Also, methods have parenthesis(), attributes don’t.

Class Trees
Before we explore these features in code, let’s have a breakdown of all the concepts we covered in the previous section. We are going to be elucidating these concepts using a Class Tree.

A Class tree with 2 instances and 3 Classes

In the Figure above there are five objects with labelled variables. From bottom-up, we have two instances named Instance 1 and Instance 2 and three classes Class 1, Class 2 and Class 3.
Just like we said in the previous section, Classes are factories for generating instances as such an Instance inherits attributes from it’s producing Class and a Class inherits attributes from a Class above it in the tree. As our figure depicts Class 1 C1is the subclass and Classes(C2 & C3) are superclasses. These definitions are based on the relative positions of each Class on the tree. The Class Tree follows a bottom-up flow hence the Superclasses behaviour is shared by all its sub-classes below it in the tree.

From our Class Tree, say we build-up and instantiate instance I2 with attribute D i.e:
I2.D
What happens is that an inheritance is effected which triggers a search in the Class tree. Python will search for the attribute D by following the tree path of I2. It follows the path like this:
I2, C1,C2,C3
and the search ends at C3 where the attribute D is found. This shows how I2.D maps to C3.D which illustrates that the Instance I2 inherits the attribute D from C3.

Also, say we want to access the attribute A in instance I1 i.e
I1.A
What happens here is the search stops at C1.A, as C1 is below C2 and C3 and it overrides them both. As such the attribute A is inherited from C1 into instance I1.

Coding Class Trees
Now let’s piece everything we’ve covered so far using some code.
Before that let’s cover some ground truths to aid our understanding.

  • A Class is instantiated using the class word
  • Each class statement generates a new class object and assigns it a name.
  • Each time a class is called it generates a new instance object
  • Instances are automatically linked to the classes from which they are created
  • Classes are automatically linked to their superclasses according to the way they are listed in the parenthesis of the class order i.e from left to right just like our class tree figure depicts.

To get started with code let’s define a class called Parent.
Classes are defined just like a function with or without parenthesis and a colon after the class name (():). Also, the body of a class is indented just like that of functions.

The __init__ method is the constructor method used to initialize the objects’ state. It is automatically called any time an instance of the class is created. The first argument, self in the __init__ method is a special convention in Python representing an object instance. This is because classes are factories for generating multiple instances as such their methods go through this automatically passed self argument anytime we need to fetch or set attributes of the particular instance being processed. So whatever instance that being called is always implied using the self argument.

The code snippet above shows a continuation of the Class we had created above and we added a new method to print whatever argument that the instance receives.

We created two instances x and y that takes in different arguments a string and an integer. Just as explained above what happens here is that the self argument represents whatever instance that is implied, be it x or y and stores the arguments in the instance namespace. So when the display method is called on each instance they both return their respective arguments which show that instances are independent entities despite being created by the same class.

Now let’s explore the parent-child relationship to make our example more relatable using classes.
Next, we are going to add more arguments to the constructor that will store information about a Parent such as last name, eye colour and race. Then we define a new method that displays the argument we received when initiating our Parent class.

Let’s create an instance of our Parent class. My name is Bukunmi Ige, so the parent class will be for my Parents, hence I will create an instance with my surname. Then we call the parent_show_info() method on our instance

Next let’s create a Child class, which will represent me the Child. This Child class will be a superclass that will inherit arguments from the Parent class which will be the subclass.
Now we are creating this Child class based on certain assumptions like The parent and the Child will share some features like Lastname, race and eye colour. The Child class will inherit arguments from the Parent class while also adding its own new arguments. In this case we will be adding cars as the new argument.

Then we create an instance of the Child class.

From our instance, we can see that just initiating the Child Class calls both the Child and Parent class.
Let’s call the show_info() method on our instance.

By virtue of inheritance, we can access the parent_show_info() method to gain information about my parents through our Child instance.

Conclusion
So far so good we covered the important concepts of Python Classes and OOP. We built upon this by exploring a simple but real example of a parent and child relationship to further elucidate these concepts. In the end, we hope this has article given you a high-level introduction to the concept of OOP and classes in Python and hope that it has also gotten you excited to do some more digging on the topic.

--

--