Skip to content
derpx06Notes on systems, models & learning
5. Data Work · lesson 15 of 17 · 1 min · January 1, 2024

Object-Oriented Programming

Defining classes and encapsulating data.

Like many languages, Python allows you to define classes that encapsulate data and the functions that operate on them. We’ll use them sometimes to make our code cleaner and simpler. It’s probably simplest to explain them by constructing a heavily annotated example.

Imagine we didn’t have the built-in Python set. Then we might want to create our own Set class.

What behavior should our class have? Given an instance of Set, we’ll need to be able to add items to it, remove items from it, and check whether it contains a certain value. We’ll create all of these as member functions, which means we’ll access them with a dot after a Set object:

set_class.py
# by convention, we give classes PascalCase names
class Set:
  # these are the member functions
  # every one takes a first parameter "self" (another convention)
  # that refers to the particular Set object being used

  def __init__(self, values=None):
      """This is the constructor.
      It gets called when you create a new Set.
      You would use it like
      s1 = Set()              # empty set
      s2 = Set([1,2,2,3])     # initialize with values"""

      self.dict = {}  # each instance of Set has its own dict property
                      # which is what we'll use to track memberships
      if values is not None:
          for value in values:
              self.add(value)

  def __repr__(self):
      """this is the string representation of a Set object
      if you type it at the Python prompt or pass it to str()"""
      return "Set: " + str(self.dict.keys())

  # we'll represent membership by being a key in self.dict with value True
  def add(self, value):
      self.dict[value] = True

  # value is in the Set if it's a key in the dictionary
  def contains(self, value):
      return value in self.dict

  def remove(self, value):
      del self.dict[value]

Which we could then use like:

using_set.py
s = Set([1,2,3])
s.add(4)
print s.contains(4)     # True
s.remove(3)
print s.contains(3)     # False