POOP Introduction#

In this module, I will be discussing P ython O bject O riented P rogramming, or POOP for short.

Before OOP#

The conceptual understanding and method for programming that is often used is called procedural programming. Backend components may run code in parallel, or my operate using fancy technology, but the code is written procedurally. This means that is is executed or written with the intent that each line will be executed sequentially.

There are a few different examples that illustrate this explicitly. Let’s take a look at the R Programming Language.

# libraries
library(dplyr)
library(tidyr)

# data
data(iris)
head(iris)

# pipe operator
m <- iris %>%
  group_by(Species) %>%
  summarize(mean=mean(Sepal.Length))

# print
print(c("Mean Sepal Length for setosa" = m[1, 2]))

In this example, we are using %>%. This is called a chain operator. It sends the output from one execution of code to the other. Let’s break this down line by line.

m <- iris %>% # this assigned the iris df to a variable, m. %>% tells it that we want to do more
    groub_by(Species) %>% # Here, we are grouping the data by species type and planning on doing even more
    summarize(mean=mean(Sepal.Length)) # Here, we are getting the mean sepal length for each group. 

Because we have used %>% we do not assign the entire data frame iris to the variable m, but rather, assign the output from our subsequent code to m. In this case, the means of sepal lengths for each species are assigned to the variable m. Let’s take a look at another example of procedural programming with Unix.

# enter current path
x=pwd
cd $x

# make file with some text
echo "Hello world" > file.txt

# use pipe
cat file.txt | grep "l" | wc

Here, we are using |. This is called the pipe operator. The output of one command is assigned to the input of another command. Let’s take a look at this line by line.

x=pwd # this finds the current working directory and assigns it to variable x.
cd $x # This allows us to enter the current working directory
echo "Hello world" > file.txt # this creates a file named "file.txt" with "Hello world" as the first line.
cat file.txt | grep "l" | wc # cat <file> reads in the file. `|` Tells us to do more. grep "l" is saying to get all the information of strings that contain the letter "l". wc is word count information

We have used the pipe operator to assign outputs from one command to the inputs of another. But the two examples which I have provided are overly pedantic to get a simple point across. In fact, they may have served you better for other coding functions, operations etc. So to justify the simplicity of procedural programming (at least for the extent of this module), I will provide one more exapmle with Python.

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in x:
    if i % 2 == 0:
        print(f'The number %d is even.' % i)
    else:
        print(f'The number %d is odd.' % i)
The number 1 is odd.
The number 2 is even.
The number 3 is odd.
The number 4 is even.
The number 5 is odd.
The number 6 is even.
The number 7 is odd.
The number 8 is even.
The number 9 is odd.
The number 10 is even.

The above example is also considered procedural programming. It executes each line of code sequentially. If I have not made myself clear, practically all code is written procedurally*.

*Here, I am explicitly referring to the context of a student using coding in neuroscience. If you are in a different, field, this may or is likely different.

Object Oriented Programming#

For me, OOP was a difficult concept to initially wrap my head around. Coming from a non-tradtional coding background, I had grown overly accustomed to proceduraly programming. It’s easy to develop and understand from an outsider persepctive. But OOP is has a lot of advantages over procedural programming. I would say that it took me a good month to be comfortable with developing my own POOP code–but after that, it has gotten loads easier. The goal of this module is to

  1. Demonstrate POOP

  2. Show you how to build POOP

  3. Explain the advantages of POOP and when to use it

Continue to the next section to get an in-depth look at POOP.