Object Oriented Programming for the Programming Challenged part 1

Kathleen B
4 min readSep 8, 2019

Another programming concept I try to explain to help myself understand it better! If you come across this I hope that it helps too!

What is OOP and why is it so important?

Object Oriented Programming (OOP) refers to a type of computer programming in which programmers define not only the data type of a data structure, but also the various types of functions that can be applied to it. Some languages are object-oriented such as Java and C++ and even Python.

Object Oriented Programming is useful because it helps keep the code more maintainable, reusable, better productivity, just to name a few. It also helps when dealing with large amounts of data and such.

Classes and Instances

Each object in Python is an instance of a class. Python data structures such as numbers, strings, and lists represent something simple like numbers, and strings. But if you wanted to represent something complicated this is where OOP would come in. Classes are used to create new user-defined data structures that contain arbitrary information about something.

The way most explain what classes are in OOP is that it’s a blueprint and an idea of how something should be defined.
Source: Object Oriented Programming in Python 3

An instance would be the object of a class, I’ll be breaking it down a little bit more in the next part.

Breaking Down Parts of the Code

When you define a class in Python you would do this:

class Dog:
pass

When you define a class you use the keyword : class to indicate that you are creating a class, and you name it. In the above example, Dog is our class. For the sake of having our code run without errors, the word pass is added in as well. Pass is used as a placeholder, if we don’t put that in we would get an error.

corgi = Dog()
print(corgi)

When this gets printed, corgi is an object (instance) of the class Dog. If that’s confusing, you can think of it this way: A corgi is an instance of the class of objects known as dogs. In a way you’re getting a bit more specific. Another example would be if we were to declare class console, an instance of it would be like PS4, Xbox, etc. Think of it as if the class is the bigger, general picture and the instance is an example of it.

class Dog():
def __init__(self, name):
self.name = name

So for our dog class, I’m going to have one attribute for it. Attributes are specific to each object. You can pretty much give as many attributes for it. But for this just one is just fine. When creating the dog object, we set the attributes we want it to have, so our class receives data once we instantiate it.

Looking at this part of the code, we’re using the init method. The __init__ method is a method in python classes, and is a contructor in OOP. I’m gonna declare another class, this time it would be for car.

class Car():
def __init__(self, numberofdoors, numberofwindows, engine):
self.numberofdoors = numberofdoors
self.numberofwindows = numberofwindows
self.engine = engine

So the init method initializes your car. Think of it as you just built a car. You need a car before you can figure out how many doors you want, how many windows it has or what type of engine it has too. Same thing applies to the dog class that I declared earlier, you can’t really have a name for a dog unless you actually have a dog. (Technically you can, but the car example is a better example for explaining this part of OOP).

The other part of the code, self , is the reference to the Car before its built, or in the dog class before its there. It pretty much says hey, this car is going to have doors, windows and an engine.

class Car():
def __init__(self, numberofdoors, numberofwindows, engine):
self.numberofdoors = numberofdoors
self.numberofwindows = numberofwindows
self.engine = engine
Tesla = Car(2,4,'electric')
print("The Tesla has {} doors {} windows and has an {} engine".format(
Tesla.numberofdoors, Tesla.numberofwindows, Tesla.engine))

Now we’ve put it together in our car class. The next part where we declare “Tesla” we are instantiating the Car class with the various attributes. The next part where we print it out. The output of this will look like this:

I don’t know much about Tesla’s so the information I use here to instantiate the class is just an example haha :D

That’s it for now!

In this part I’ve explained and broken down the code in OOP. I’ve showed how to declare a class and broke down the various methods used in OOP. Later on, I’ll write another part about other concepts regarding OOP, explaining Inheritance, Encapsulation. Funny enough after writing this little entry I actually understand Object Oriented Programming a bit more!

I’ll be writing another part of this later on, but if you stumble upon this I hope it helps!

--

--

Kathleen B

Food Scientist turned Front End Developer. I talk about coding things. I also like lifting heavy objects and anime.