Beginner’s Guide to O.O.P

Isac Simkin
The Startup
Published in
3 min readMay 23, 2020

For this guide, I am assuming you have previous basic programming knowledge in Python, especially with implementing functions with arguments.

Classes and objects

In simple words, a class is a model of something you want to recreate in code. Think of it as creating the general concept for whatever is it that you are trying to bring to life (digitally). For example, if you want to create a car, you need to establish what parts it needs to have to function the way you intend it to work.

A class would be the general concept of that car: the abstract idea that has wheels, windows, doors, engine, so on and so forth. Moreover, an object would be a specific version or an instance of the class. For example, a black car with 4 wheels, 6 windows, 4 doors, an engine, so on and so forth.

For a better visual look at the image below. The car on top represents the Class and each of the cars in the bottom represents objects based on the Car Class. All of the objects have the same overall structure but differ in their individual attributes.

https://intellipaat.com/blog/tutorial/python-tutorial/python-classes-and-objects/

General Structure

In the code below, the objectmyCar inherently has all of the attributes any car created based on the Car class has to have. In Python, a simple class would look something like this: (NOTE: the numbers are row numbers, they are NOT part of the code)

1 class Car:
2 # Initializer
3 def __init__(self, color):
4 self.color = color
5
6 myCar = Car("Black")

Let’s break it down:

  • Line 1: starts the creation of a class.
  • Line 2 : the # refers to the start of a comment. Everything in that line will be ignored by the compiler.
  • Line 3 : this is the signature of the__init__ function which serves as the constructor. It initializes the Car class.
  • Line 4 : assigns values passed as arguments to the attributes of the class.
  • Line 6 : contains the variable myCar which will store an Object with the color attribute “Black”.

In the code above, a Car only needs the color argument for it to be initialized. However, if you were to require more than one attribute to create a Car, it is possible to add them as a part of the __init__ function. See below:

1 class Car:
2 # Initializer
3 def __init__(self, color, make):
4 self.color = color
5 self.make = make
6
7 myCar = Car("Black", "Toyota")

In comparison with the previous block of code, the Car class now has two attributes: color and make. Now each object based on the Car class would need to be created with both attributes rather than only a color.

Adding functionality to the class

Furthermore, a car is not really functional without it being able to turn on, move forwards, backward, and turn left or right. In coding terms, this “ability” for a class, and the objects rooted from it, are called functions. These are what give life to an object and enables it to “do” things. Let me illustrate this with an example:

1  class Car:
2 # Initializer
3 def __init__(self, color):
4 self.color = color
5
6 # function
7 def moveForward(self):
8 print ("The car moved forward")
9
10 myCar = Car("Black")
11 myCar.moveForward()

We can give myCar the “ability” to move forward with the moveForward function defined after the __init__function and calling it with myCar.moveForward() .

Class attributes

Even though most of the attributes can be determined when creating an object, some others are predetermined and set by the class itself. For example, every car needs to have an engine in order to function properly. To guarantee that each object has an engine, you specify it in the class itself. On line2 , we are establishing that every object from that Class is going to have exactly 1 engine.

1  class Car:
2 numberOfEngines = 1
3
4 # Initializer
5 def __init__(self, color):
6 self.color = color
7
8 # function
9 def moveForward(self):
10 print ("The car moved forward")
11
12 myCar = Car("Black")
13 myCar.moveForward()

Thank you for reading over this beginner’s guide to Object-Oriented Programming. I tried to make it as simple and straight forward as I could, so please let me know if something needs more explanation.

--

--