So what even is POOP?#

The previous section only provided you with what OOP isn’t. In this section, I will be going over the basics of object oriented programming like…

  1. What is it?

  2. How to construct a simple object in Python

So what is an object?#

Well, it’s not anything physical. Think of it like a metaphorical toychest. The chest is the object. Inside they chest, there are toys. You can do different things with these toys. In Python, objects are called classes. They toys are called methods and attributes. A method is like a toy that does stuff, like a car that moves, or legos that can be built. Attributes are more like figurines or barbie dolls: you mainly just look at them. Attibutes are primarily descriptive.

Lots of Python packages are built using an OOP framework. A popular one in neuroscience, MNE, uses OOP to run analysis pipelines. You can take toolboxes, packages, modules, etc. built with OOP and using them procedurally. or dynamically.

What does an object look like in Python?#

I will be building an example class using a car. The car is the object. It has different attributes, like color, size, miles per gallon, etc. It also has methods, like driving, parking, reversing and flying (just for fun).

Just like when you define a function in Python, you need to define your class.

class Car

Once you have defined your class, you need to do a few things more steps. This is where things become more tricky because you are having to conceptualize objects in more detail now and understand the components which make up an object.

We now need to create a special function used specifically for objects, called self. It looks lik this.

def __init__(self):

It is under this function that you can initialize some attributes to the object. Lets look at what this is.

class Car:
    def __init__(self):
        self.color="Red"
        self.make = 'Porsche'
        self.model = "911"
        self.year = "1966"

car = Car()
print(f'The make of this car is {car.make}.')
The make of this car is Porsche.

As you can see, I have attributed this data to the object. A few things to note syntax wise:

  1. When assigning an object to a variable, you must include parentheses at the end, like car = Car().

  2. When calling a method, you also use parentheses. See the next cell for an example of constructing a method.

  3. When calling attributes, you do NOT need parentheses.

Now that you’ve seen what attibutes look like, let’s take a look at some methods we could implement in this simple example.

class Car:

    # define self and attributes
    def __init__(self):
        self.color="Red"
        self.make = 'Porsche'
        self.model = "911"
        self.year = "1966"

    # Method 1: Make car go forwar
    def drive(self, distance):
        """Make the car drive
        
        Parameters
        ----------
        distance: int
            How many miles are you going to make the car drive?
        """
        self.distance = distance
        print(f'Driving %d miles.' % self.distance)

car = Car()
car.drive(distance=100)
Driving 100 miles.

Methods are functions within the class that do things with the object. When defining your function, you need to include self in that function. This allows you to pull other components of the object into that function directly, like the attributes or other things that may have developed along the way in other methods. Like in this case: we require a parameter for Car.drive(), distance. The function then uses this to print a statement. But, we are also assigning that argument to self and making it an attribute. Try calling car.distance. It should print out the parameter that you gave it.

I’ve also included a doc string. This is a good habit to get into. Even though the method/function which we’ve defined is extremely simple, it is good practice to write this docstring. Docstrings can be called via .__doc__

# calling new attribute, distance
car.distance
100
# printing docstring
print(car.drive.__doc__)
Make the car drive
        
        Parameters
        ----------
        distance: int
            How many miles are you going to make the car drive?
        

Let’s build some more complicated objects.