Get startedGet started for free

What is OOP?

1. What is OOP?

Hi! I'm George, and I'll be walking us through the fundamentals of object-oriented programming, or OOP!

2. Procedural vs OOP

Until now, we have likely been coding in procedural style: our code was a sequence of steps to be carried out. This is common when performing simple, non-repetitive tasks, or for data analysis, where we load data, explore it, and produce insights.

3. Thinking in sequences

Procedural thinking is natural and how we tend to plan our days. But a software engineer, like a city planner, needs to think about thousands of people with their own routines. Mapping out a sequence of actions for each individual would become unsustainable. Instead, we should think about patterns of behavior.

4. Procedural vs OOP

This is what OOP involves - thinking about objects and patterns of their interactions - like users interacting with elements of an interface. This point of view becomes invaluable when designing and building software like Python packages! It helps us organize our code better, making it more reusable and maintainable.

5. Objects

The first fundamental concept of OOP is an object, which is a combination of data and functionality. For example, an object representing a customer can have data such as a phone number and email associated with them, and functionality like the ability to place or cancel an order. Data associated with an object is known as the object's state. The functionality of an object is known as its behavior. In OOP, state and behavior are bundled together. So, instead of thinking about customer data separately from customer actions, we think of them as one unit representing a customer.

6. Objects in Python

In Python, everything is an object. Numbers, strings, DataFrames, and even functions are objects.

7. Classes as blueprints

But how do objects get created? This leads to the other core concept of OOP, classes. Classes are like blueprints for objects. They describe the possible states and behaviors that every object from that class can have. Therefore, Customer objects are made from a Customer class. To enable Customer objects to have states such as email address and phone number and behaviors including placing and canceling orders, they must be defined in the Customer class. The class allows us to talk about customers in a unified way.

8. Classes as blueprints

An object is an instance of a class. So an individual customer is an object created from our Customer class blueprint.

9. Classes in Python

The unified templates that classes create are what allow Python objects of the same type to behave in the same way. For example, all lists can store data in the same way, through comma-separated values, and have the same methods, such as dot-append. To find out the class of a Python object we can call the type function. Here, we confirm the class of comma-separated values inside square brackets is a list.

10. Attributes and methods

Remember that in OOP, state refers to data associated with an object. In Python, this is represented by attributes. As an example, every pandas DataFrame has a "shape" attribute that we can access by specifying the name of the DataFrame, then dot-shape. Meanwhile, behavior reflects an object's functionality. In Python, this is represented by methods. An example is the dot-head method of a pandas DataFrame, allowing a preview of the first five rows.

11. Displaying attributes and methods

We can display all the attributes and methods by calling dir on a specific object. For example here, we see that a list has methods like pop and sort. This also works on a class. We'll discuss why some methods and attributes are surrounded by underscores later in the course.

12. Cheat sheet

We covered a lot of new terms in this video, so please use this cheat sheet for reference!

13. Let's review!

Head over to the exercises to review everything!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.