Classes in Python
In this video, 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
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?
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
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.
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.
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.
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.
Become a Member to join the conversation.