In this segment of the tutorial, the instructor transitions from using NumPy arrays to introducing object‑oriented programming (OOP) as a way to build more complex data structures such as Pandas DataFrames. He explains why functions and standalone variables are insufficient when the same array needs to be created repeatedly or referenced across different parts of a program, and argues that classes provide a reusable template that bundles related variables (attributes/fields) and functions (methods). The talk defines OOP terminology—attributes, class variables, methods—and uses a cookie‑cutter metaphor to clarify the relationship between a class (the template) and its objects (the instantiated cookies). The instructor then demonstrates the basic syntax for declaring a class in Python 3, noting the optional parent‑class parameter used in Python 2 code, and shows how to instantiate an object (e.g., a Tyrannosaurus dinosaur) from the class. The segment ends with a promise of further examples and a preview of inheritance and polymorphism later in the course.
Chapters
We've now taken a look at numpy arrays. But how do we get from a single numpy array to building a Pandas dataframe? The answer is classes, which we'll start exploring in this section. So far we've been using functions and variables as one offs. But what if we want to create the same ndarray multiple times or reference it in different pieces of code? Or what if we want to build some other data structure on top of our ndarrays? A class will help with that. What is a class? It's a reusable chunk of code that has functions and variables. As we reviewed in the previous section, we'll want to use OOP when we create groups of code that have both variables and functions we want to reference. Much like a recipe helps us to plan to how to actually cook our meal, classes will allow us to be able to create our work easily. You may remember back to section one where we created a sample class that allow us to create the same list every time. Python has its own special vocabulary that we've used in Imperative Functions and Variables. When we create a recipe to follow, the terms in the recipe have special names. On the left hand side of the picture are the imperative names. On the right hand side are the OOP names. A variable in a class is called an attribute or sometimes a field. They can also more generally be called class variables. A function in a class is called a method. But what is a class and what's the connection to the object from oop? Think of classes as cookie cutters and objects as the actual cookie that results from using the cookie cutter and based on a set of instructions. For example, if we have a Tyrannosaurus cookie cutter template, we can use it to make lots of different dinosaurs. They'll all have different names, but they'll share the properties of the original cookie cutter template. They'll all have the same legs, arms and teeth. Once we create a single class or template, we can use it as many times as we want, creating different objects based off the parent class. Later on in this course, we'll look at how to create objects that are different through inheritance and polymorphism. For now, we need to understand that a class is a template for an object. Let's start by declaring a class. Declaring a class is not unlike declaring a method, except instead of death, we have the special Python keyword class followed by the class name. The pass in the second line means we're not putting any context or values into the class yet. And when the program evaluates the code, it will pass over it and skip to the next available piece to execute. What goes in the parentheses? A parameter that specifies what kind of class we have. For Python 3 classes, we don't need to specify this, but for Python 2 code bases, it's still a practice to put object in as a parameter. What does it look like when we're assigning an object? Like the last piece of code on the slide, we reference a class dinosaur as we create an instance of a class called Tyrannosaurus. We'll dive into this more deeply. Coming up. For now, let's try some examples.