Introduction to Object Oriented Programming

Welcome to part 13 of the intermediate Python programming tutorial series. In this tutorial, we're going to introduce the concept of Object Oriented Programming (OOP), which is a topic that will be present in quite a bit of the rest of this entire series. Almost immediately, you will be able to see some of the benefits of OOP, but OOP is also a gift that keeps on giving.

Object Oriented Programming is a hard thing to define, but it's centered around the creation of objects and interacting with them, as you might guess. Objects have characteristics and features, known as attributes, and can do various things, through their methods. The biggest feature of OOP is how well objects can be interacted with, and even molded in the future, which makes them very friendly to developers, scale, change over time, testing, and much more.

I think to understand OOP, it's best you just do it, however, so let's jump in. We're going to use PyGame. In most cases, you can just do pip install pygame and you're all set. If you're on Mac, life is a bit more challengin, but you can check out PyGame on macintosh instructions, or try poking around on Google for more solutions.

I want to make use of PyGame, since it gives us a simple way to actually visualize what we're doing and building, so we can see our objects in action. What we're going to do is build Blob World, which is a world that consists of actors, known as blobs. Different blobs have different properties, and the blobs need to otherwise function within their Blob World environment. With this example, we'll be able to illustrate quite a few OOP concepts. Let's begin!

A Class is what we need to create an object, so we'll begin with a Blob class. Python's objects have a bunch of "special methods" often called magic methods. The most common one of these is the __init__ method, but there are many others, many of which we'll touch on. The __init__ method (pronounced: "dunder init method", where "dunder" describes the double underscores) is a method that we can use to specify anything that we want to happen when the object is initialized. Any time someone defines a variable as being an object of the Blob class, the dunder init method will run. Let's populate this method with some basic information, like maybe a starting location for the blob, a size, and a color of the blob:

class Blob:
    def __init__(self, color):
        self.x = random.randrange(0, WIDTH)
        self.y = random.randrange(0, HEIGHT)
        self.size = random.randrange(4,8)
        self.color = color

The init method has two arguments: self and color. The self argument can be called *anything* you want, but "self" is the convention. self is the instance object. We will use self. to create and access an object's attributes from within the class, as well as outside the class (where the object's variable name takes the place of "self"). The color argument is going to be our only variable for now. Now we can add other regular methods too. Maybe we want our blobs to be able to move?

    def move(self):
        self.move_x = random.randrange(-1,2)
        self.move_y = random.randrange(-1,2)
        self.x += self.move_x
        self.y += self.move_y

The idea of this is that it's going to interact in a limited environment, with a fixed width and height, so we'll go ahead and add some handling for if this blob happens to exceed boundaries:

    def move(self):
        self.move_x = random.randrange(-1,2)
        self.move_y = random.randrange(-1,2)
        self.x += self.move_x
        self.y += self.move_y
        if self.x < 0: self.x = 0
        elif self.x > WIDTH: self.x = WIDTH
        if self.y < 0: self.y = 0
        elif self.y > HEIGHT: self.y = HEIGHT

In the next tutorial, we're going to use PyGame to create the environment for this object.

The next tutorial:

  • Intermediate Python Programming introduction
  • String Concatenation and Formatting Intermediate Python Tutorial part 2
  • Argparse for CLI Intermediate Python Tutorial part 3
  • List comprehension and generator expressions Intermediate Python Tutorial part 4
  • More on list comprehension and generators Intermediate Python Tutorial part 5
  • Timeit Module Intermediate Python Tutorial part 6
  • Enumerate Intermediate Python Tutorial part 7
  • Python's Zip function
  • More on Generators with Python
  • Multiprocessing with Python intro
  • Getting Values from Multiprocessing Processes
  • Multiprocessing Spider Example
  • Introduction to Object Oriented Programming
  • Creating Environment for our Object with PyGame
  • Many Blobs - Object Oriented Programming
  • Blob Class and Modularity - Object Oriented Programming
  • Inheritance - Object Oriented Programming
  • Decorators in Python Tutorial
  • Operator Overloading Python Tutorial
  • Detecting Collisions in our Game Python Tutorial
  • Special Methods, OOP, and Iteration Python Tutorial
  • Logging Python Tutorial
  • Headless Error Handling Python Tutorial
  • __str__ and __repr_ in Python 3
  • Args and Kwargs
  • Asyncio Basics - Asynchronous programming with coroutines