Data Visualization With Python

19. Object-Oriented Programming

Object-oriented programming (OOP) is an approach to organizing code into classes and objects, and it enables programmers to connect data to its behavior easily. All three programming languages covered in this event support OOP, although some can also support other programming paradigms. OOP is the most dominant modern programming paradigm because it’s designed to encourage two important computer science concepts:

  • Modularization - breaking down a major application into many small “modules”
  • Re-using code - writing code that others can reuse, enabling programmers to collaborate

Classes

An OOP class is a way to organize concepts that have certain notions in common. All objects built off a class will share essential characteristics as well as differ from one another. A close analogy is an animal species; all dogs share the “class” characteristics of being able to run, having four legs, and fur, yet still differ in the “object” characteristics of their running speed, the length of their legs, and the color of their fur.

Classes have two major components:

  • States: data that is stored in the class (but may not include the values of the data)
  • Methods: the behavior of the class (how the object interacts with its data; methods are shared across all objects built using the same class, but they will behave differently depending on the object’s state)

Objects

Objects are an instance of a class, meaning that they realize the concepts outlined in a class. Using the animal analogy, a class of dogs contains all the features (states) that all dogs have, but a dog object will have concrete parameters that make that specific dog unique compared to other dogs. A dog class might contain a state for height, but a dog object will contain the actual height; some dogs will be taller, and some dogs will be shorter.

For example, we could build user accounts on this website using classes and objects. Our class includes states and methods all users have:

  • States (data that is stored in the class) - first name, last name, current point value
  • Methods (the behavior of the class) - adding points to a user, retrieving points

Let’s write the code for this class and test it using objects built from the class.

class user:
    # states
    firstName = ""
    lastName = ""
    points = 0 # all users start with 0 points
    
    # constructor method that is always called when an object is initialized
    # this should only include code that every object needs to run before use
    def __init__(self, firstName, lastName):
        self.firstName = firstName # "self" keyword refers to the current object
        self.lastName = lastName
    
    # other methods
    def addPoints(self, addedPoints):
        self.points += addedPoints


# this code tests our class
JLo = user("Jennifer", "Lopez")
ARod = user("Alex", "Rodriguez")

# test our addPoints method
JLo.addPoints(5)
ARod.addPoints(10)

print(JLo.points)
print(ARod.points)

Notice that we have two objects, one representing JLo and one representing ARod, built using the user class. The class only contained the data types that the object should include, but the object contained the actual data values. In addition, note that when we modified the points of one user, it did not affect the other user.

Encapsulation

OOP encourages programmers to encapsulate their code. Encapsulation is where the mechanics behind code is hidden, but the overall function and interface to use the function are shown. For example, your television may have buttons for changing the volume, changing channels, etc. While most people do not understand the underlying mechanisms, they know how to use the interface (the remote) and use the television effectively.

Object-oriented programming is a programming paradigm highly prevalent in modern app development. It’s crucial to understand OOP for future projects.

You can play with all the code we've used in this article on Trinket: