Classes in Python – Real Python (original) (raw)

In this lesson, you’ll learn what Python classes are and how we use them.

Classes define a type. You’ve probably worked with built-in types like int and list.

Once we have our class, we can instantiate it to create a new object from that class. We say the new object has the type of the class it was instantiated from.

We can create multiple objects from the same class, and each object will be unique. They will all have the same type, but they can store different values for their individual properties.

If it helps, think of classes like blueprints that define how an object should be built. Where might we use classes and objects in real software?

00:01 Welcome back to our video series on object-oriented programming in Python. In the last video, we learned what object-oriented programming is and what problem it solves.

00:10 Now, let’s take a look at how we can define our own objects in our Python programs. To create our own objects, we use Python classes. Classes are used to create objects—practically as many unique objects as we want. They also define a type. For example, take a look at this code: my_name = 'Austin'. Here, we create a new variable called my_name with a value of 'Austin'. Behind

00:40 the scenes, this variable is actually referencing an object. The type of the object is str, short for string, which is a built-in Python data type that you’ve probably used before.

00:53 Python defines a string as a class, and in order to create this object of type str, we instantiated the str class. To instantiate a class, or to create an instance

01:05 of a class, just means to create an object from that class. You’ll hear the word instantiation thrown around a lot in programming, so just remember: it means to take a class and create an object from that class.

01:20 To make this a little bit more clear, I’m going to use one of my favorite analogies. Let’s say we are writing a program that creates doors. We’ll start by creating a class for a single door.

01:32 This class, here, is like the blueprint from which we will build all of our Door objects. The class will specify some properties and behaviors.

01:41 Remember, each of our Door objects will have these, and they’ll be independent to each Door object. A door could have properties like height, paint color, and whether or not it’s currently locked.

01:53 It could also have behaviors like open, close, and toggle lock. We’ve defined a Door class, but we’ve also just defined a new type we can use in our program.

02:04 Let’s create a Door object with a height of 85 inches, painted red, and we’ll say that when we build it, it’s unlocked. Here, we’ve just instantiated the Door class.

02:16 Remember, that means that we’ve created an object of type Door from our Door class. And we aren’t limited to creating just one.

02:25 Just like how in the real world we can build lots of doors from a single door blueprint, we can instantiate many Door objects from our single Door class. Here, I’ve instantiated one more. Notice that each of these Door objects is unique.

02:40 They both have different heights and colors, and each one can either be locked or unlocked at any time. We can change the paint color of a single Door and that doesn’t affect the rest.

02:52 These objects are all independent and they don’t rely on the class anymore. The class just tells Python how to create the objects.

03:01 Here’s a question: can you think of any scenarios where classes and objects could be used in real-world programs? In the next video, I’ll give you some examples, and then we’ll learn about how classes are actually built in Python.