Object-Oriented Programming

Understand the basics of object-oriented programming.

Before we start writing code, it’s important to understand object-oriented programming (OOP). We’ll be using OOP concepts throughout the development of the pipeline. However, instead of an academic examination of the topic, we’ll have a brief introduction and discuss concepts that are relevant to our work.

What is OOP?

When people hear "programming," most think of procedural programming. It’s the relatively simple model in which our code resides in function, also known as procedures. A function, as we know, is nothing more than a set of statements that are run in sequence.

Press + to interact
def start(car):
#...
def accelerate(car, speed):
# ...
def brake(car, speed):
# ...
def stop(car):
# ...
if __name__ == "__main__":
car = #...
speed = # ...
start(car)
accelerate(car, speed)
brake(car, speed)
stop(car)

In contrast, the central concept in the OOP model is an object. An object is an entity that contains certain attributes and methods. Attributes are variables that hold information, which may include data. An object’s attributes may or may not be accessible from outside the object—they may be public or private. Methods are functions that have access to all the attributes of the object. They form the interface between the attributes and the outside world.

Press + to interact
class Car:
def start(self):
#...
def accelerate(self, speed):
# ...
def brake(self, speed):
# ...
def stop(self):
# ...
if __name__ == "__main__":
car = Car()
speed = # ...
car.start()
car.accelerate(speed)
car.brake(speed)
car.stop()

We won’t go into the pros and cons of each programming model, but each one has its uses. In general, though, OOP has several advantages over procedural programming:

  • OOP provides a way to abstract data, meaning it allows the programmer to hide data that users of the code don't need access to.

  • OOP models solutions in the form of objects with encapsulated interfaces, which makes the code more modular.

  • Features such as inheritance make extending functionality easier.

Ther ...