+254 728 680 519

Data types and Objects Part 3

Well, I love cars a lot so get used to it! That's a 2017 Mercedes-Benz C-Class Cabriolet. Beautiful isn't it? I mean what could be more beautiful than sitting inside that cruizing at around 250 km/hr or 155 miles/hr for the 'mate'  lots. It is also german with 385 horsepower. Imagine what you can do with that baby! And speaking of babies today is Mother's day! It is a special day we are who we are because of our mothers. 

Let us say we are a reassembly factory for Mercedes car model types. We need to set up a system that will be used to differentiate the various parts of the merchandise so that we don't assemble an AMG engine where we are supposed to have a V6 engine. So what we will do is we will make these brands. We will only use the engine types and we will refer to its attributes to calculate various elements like speed. Let's get started!

So, first of all, any of the above cars have an engine so we will start by modeling the engine. An engine basically does one thing which is to start and stop. That's its function so:


In the above diagram, we have three items of python which I want you to be aware of. We have the class which we have already talked about (Engine), We have functions(start, stop) which starts with the 'def' statement. After the name of the function I have used '(self)' this means an instance of the class, which is an object. I have used the pass statement within the body of the function so that we don't have to code the start and to stop functions though in a real-life scenario you would be expected to detail this. Next, let's introduce the engine types. Look below what I dug up from the internet.

The first thing we need to do is study this data before we can model. These are the content of the data:

  • Three car brands (2019 Mercedes-Benz C 300, 2019 Mercedes-Benz C 300 4MATIC®* and 2019 Mercedes-Benz AMG® C 43)
  • Two types of engine (inline 4, v6)
  • Three engine attributes (Horsepower, Torque (lb-ft), Acceleration 0-60 mph (sec.)

Moving on swiftly below is what we have.

We have two new things here within our new classes of engines. First when we define the class after the name we put the Engine class in brackets before entering the body of the class. This functionality is called inheritance which basically means the class inherits from the parent class. This is the same concept as the way objects inherit attributes and methods from the class it is defined within. This time our functions have some work to do and that defines the attributes of the engine. As you can see we still are using self then the attribute to define the instance or object of that class.

Next, we need to have a class. A car has an engine, right? So let's introduce what a car is according to our program

We have the second functionality of objects or in this case classes. That is composition. If you have a keen eye you might have noticed earlier I said a car has an engine and the 'has an' was in bold. Well, this is the composition bit of object-oriented programming. A car has wheels, an engine, a steering wheel demonstrating what composes a car but in our case, a V_6 engine is an engine demonstrating the inheritance relationship between the engine and the v_6 engine. How do we show composition? well, when we enter into the body of the car class you do notice the assignment of engine_cls to Engine.  Then afterward we use the __init__ method to form the engine of the car. When an object is created, the __init__ method is automatically run for us in the process assigning the instance of a car with an engine as defined within the body of the car type. More on it below!

We have added our model types and see how we get the engine types using composition. When the __init__ method within car is called, what happens is that self.engine variable which is the instance of the car at the time is assigned to the engine class  which is defined within the class of the car type. 

Now how do we test our model? By creating objects of course. Take a look below:

And our output;

I know I have introduced so many concepts eg functions, iterators, compositions, etc don't worry I will list out all the areas we have seen for you take a chance to dive into them and grasp them. I had promised we will squeeze oop to 3 posts but that has proved futile we will have another 4th post where we will wrap up introduction then you can go into the syntax of what we have talked about. 

Till then adios!


Playing Basketball, Arsenal fan, Python, and coffee!